Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: BAT Script to Copy Most Recent File from One shared to Another

05 Aug 2014   #1
divyap112

windows 7 64 bit
 
 
BAT Script to Copy Most Recent File from One shared to Another

Hi,

Please help me in creating a batch file to copy file from one shared location to another. ( I have no knowledge about batch files)

I have a folder that has more than 3000 sub folders, each sub folder having 1 or more files. I want to copy only few sub folders to my destination location. List of folders that are to be copied i will get from Database and maintain in text file. i need only the folders that are mentioned in text file to be copied to destination location.
Everyday my source locations keeps on getting new folders or existing folders content keep changing. So i want them also to be copied to that destination location.

So please help me creating a batch file for doing the above.


My System SpecsSystem Spec
.
05 Aug 2014   #2
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi and Welcome to SevenForums,

Quote   Quote: Originally Posted by divyap112 View Post
List of folders that are to be copied i will get from Database and maintain in text file. i need only the folders that are mentioned in text file to be copied to destination location.
Based on the above quote you will have a txt file with a list of folders, you would like to copy them to a different network location.

Correct?

The list of folders in the txt file, will that just be the folder name of the full path to the folder?
  • Folder Name - Test_Folder
  • Full Path - Z:\Shared_Drive\Test_Folder

Regards,
Jamie
My System SpecsSystem Spec
05 Aug 2014   #3
divyap112

windows 7 64 bit
 
 

Hi,

It will have only folder name, as folder names will be unique in my case
My System SpecsSystem Spec
.

05 Aug 2014   #4
JDobbsy1987

Windows 8.1 Pro x64
 
 

ok so you would need to specify the path to the share that those folders are stored in.

You will need to update the below script:
  1. Enter the path to the share where the folders are stored where it says folderpath=
  2. Enter the path and filename of the text file with the list of folders in the for loop inside the 1st set of brackets
  3. Enter the destination path where you want the folders copying to in the 2nd set of brackets

Disclaimer - Always test the code on dummy data before using on live data
Create a couple of test folders etc...
Code:
rem This specifies the share where the folder you want to copy are stored in
set folderpath=\\server\share

rem This is the for loop top copy each folder named in the .txt file to the new location
for /f %%a in (c:\users\%username%\desktop\folderlist.txt) do (xcopy %folderpath%\%%a \\new_server\new_share)
Regards,
Jamie
My System SpecsSystem Spec
07 Aug 2014   #5
divyap112

windows 7 64 bit
 
 

Hi Thanks for your post.
Its working as expected, but I have one issue.
If my fileContents are
Folder1
Folder2
Folder3

On running the batch file, the contents within Folder1,Folder2 and Folder3 are copied. I need to create "Folder1" ( if previously does not exist) and then to that folder copy the contents of Folder1.

Thanks
My System SpecsSystem Spec
07 Aug 2014   #6
divyap112

windows 7 64 bit
 
 

Below is the query i wrote

set folderpath=E:\Project\
for /f %%a in (E:\filelist.txt) do (xcopy /e /q /d /y %folderpath%\%%a E:\Backup\)
My System SpecsSystem Spec
07 Aug 2014   #7
JDobbsy1987

Windows 8.1 Pro x64
 
 

Come to think of it i think xcopy is funny copying specific folder...
This may be a dirty work around but see if it works for you

Code:
set folderpath=E:\Project\
for /f %%a in (E:\filelist.txt) do (mkdir E:\Backup\%%a & xcopy /e /q /d /y %folderpath%\%%a E:\Backup\%%a)
This essentially makes the folder first, if the folder already exists it won't create it as the directory already exists.
Have a play on some dummy data and see how that goes.

Regards,
Jamie
My System SpecsSystem Spec
07 Aug 2014   #8
divyap112

windows 7 64 bit
 
 

Thank you so much.. that solved the problem.
My System SpecsSystem Spec
08 Aug 2014   #9
JDobbsy1987

Windows 8.1 Pro x64
 
 

Glad to hear that has helped.



Regards,
Jamie
My System SpecsSystem Spec
Reply

 BAT Script to Copy Most Recent File from One shared to Another




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
How to script a daily copy of files w/dt being name of archive folder
Hello, I need to figure out a Windows 7 script to copy/archive files which are located in a specific folder. It needs to copy daily to an archive folder, with the script creating the archive folder as MM-DD-YY (being that day's date that is being archived) before it archives the files to that...
General Discussion
Can't copy files to shared Homegroup Start Menu
I have a subfolder under C:\ProgramData\Microsoft\Windows\Start Menu on my desktop that I want to copy to the same folder on a laptop which is part of my home network. I've explicitly set the shared properties of ...Start Menu on the laptop to Homegroup (Read/Write). Nevertheless, when I try to...
Network & Sharing
how do I copy a BIG single file (using richcopy?) big file copy
I'm trying to copy a VHD (22g, dynamic I think...) I tried richcopy. It says copy started, but then stops.... I saw someone mention xcopy /j, to achieve the unbuffered I/O perf. you get with richcopy... it's running, but no indication as to how long it takes (EDIT: just finished, > 10...
General Discussion
How to run a .bat (script) file
Hi All, I have two .bat files (script) that I'd like to run from their location in command mode with admin. privl. How do I do this? Best, ms
General Discussion
VBS script used to overwrite, now makes (unwanted) copy
I have a simple VBS login script for all my users that checks a location on our server and updates (by overwriting) the same files on the users' PCs if the server copy is newer (FSO.CopyFile etc). This has run swimmingly for several months now, but today I discovered it ISN'T overwriting the local...
General Discussion
Shared files visable but can't copy
I have a couple of desktops and laptops connected on a wired network. I have no problems accessing and copying files. This isn't about access denied or other machines not being visible. My issue is that one of my laptops can be accessed and I can move through all shared folders but sometimes when...
Network & Sharing


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 11:23.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App