Monday, July 30, 2012

Script to Copy Specific Folders From One Directory to Another

I had a need to copy specific folders from a folder share to another folder share. I found a script that I had to tweak a little but it will take the contents of a txt file and copy only the folders that you put in it using Robocopy.

For those of you unfamiliar with robocopy, its a copy utility on Windows 7 that has many, many options to copy files and folders from local drives, mapped drives and even UNC paths to a destination path. If you are an old DOS guy like me, just think of xcopy on steroids.

Here is the code:


@echo off
for /F "tokens=*" %%A in (dirlist.txt) do (
  robocopy Y:\%%~A\ Y:\%%~A\ /E /R:1 /W:1 /COPY:DATS
)

Save this to a bat file (ie, copy2dir.bat) and run the bat file. Lets go over the script:

dirlist.txt is where you will put the folder names (new line for each one)

The script will take each line in the txt file, look for a corresponding folder and then copy it to the destination folder. In my case I mapped a shared drive to my Y: drive letter and used that. You could use a UNC path or local drive letter as well.

Robocopy has MANY options. Do a robocopy /? at the command prompt and you will get a listing. The options I chose were:

/E - This copies sub directories, even empty ones.
/R:1 - Retry files only 1 time. the default is literally 1 million times!
/W:1 - Wait only 1 second between tries. Default is 30 seconds
/COPY:DATS - this copies Data, Attributes, Timestamps and Security (NTFS ACLs)

You have many more options to chose from including logging.

Hopefully you may find a use for this simple script.

No comments:

Post a Comment

Error 1312 when adding ssl cert

 If you get an error when using netsh to add a cert thumbprint, make sure you have a private key attached to the cert. Also, make sure the c...