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


  1. Posts : 5
    windows 7 64 bit
       #1

    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 Computer


  2. Posts : 2,177
    Windows 8.1 Pro x64
       #2

    Hi and Welcome to SevenForums,

    divyap112 said:
    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 Computer


  3. Posts : 5
    windows 7 64 bit
    Thread Starter
       #3

    Hi,

    It will have only folder name, as folder names will be unique in my case
      My Computer


  4. Posts : 2,177
    Windows 8.1 Pro x64
       #4

    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 Computer


  5. Posts : 5
    windows 7 64 bit
    Thread Starter
       #5

    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 Computer


  6. Posts : 5
    windows 7 64 bit
    Thread Starter
       #6

    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 Computer


  7. Posts : 2,177
    Windows 8.1 Pro x64
       #7

    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 Computer


  8. Posts : 5
    windows 7 64 bit
    Thread Starter
       #8

    Thank you so much.. that solved the problem.
      My Computer


  9. Posts : 2,177
    Windows 8.1 Pro x64
       #9

    Glad to hear that has helped.



    Regards,
    Jamie
      My Computer


 

  Related Discussions
Our Sites
Site Links
About 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 03:28.
Find Us