At work today, I had to copy hundreds of files from two different network folders to my local computer (Windows PC). The manual way would have taken all day and been painfully boring…so I scripted it! Here’s how you can copy files from multiple folders using ROBOCOPY and a text file list.
The Setup
Remote
I have a list of files on a remote server directory that I want to copy locally, but I don’t want to copy all the files in that directory. There may be 10,000 files in there, but I only need the 1,000 files I have listed in “filelist.txt”.
Local
The directory I’m copying to locally is C:\Copied. I want to maintain the same folder structure as the remote server, so I’ll use C:\Copied%src% as the destination, which comes out to be C:\Copied\Folder1\Documents.
The Script
@echo off set directory=\\remoteserver\stuff\documents set src=\Folder1\Documents set dest=C:\Copied%src% for /f "delims=" %%i in (filelist.txt) do robocopy %directory%%src% %dest% "%%i" echo. echo Press any key to end... pause> nul
I tested my script first with just a few files listed in filelist.txt, just in case something went terribly wrong. Once you verify that it works for a few files (you can see the ROBOCOPY output before you close the command line window on your test run), just add the rest of the file names to the filelist.txt and fire it up!
3 Quick Notes
First, when you’re working with remote server shares, you must take file permissions into account, so make sure your account has access to the files you’re copying.
Second, if your directories have spaces in their names, you’ll need to use quotation marks (” “) as needed:
"%directory%%src%" "%dest%"
Lastly, make sure you pay attention to your own directory structure. All of the above is just a generic example meant to help you see how it’s done.
Did This Work For You?
Leave a comment below to let me know if this helped you or not. Feel free to ask a question or post any problems you are having with your script. I’ll do my best to respond and help out.
nice script
would love a version that would copy contents of the folders listed in the text file
in other words just copy the selected folders
Hi Doug. Thanks for the question! This is more what robocopy is made for that what my script was above. I would suggest you read all the switches for robocopy here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
In general, if you don’t want to get a copy of everything in a directory, you could try something like this:
@echo off
set directory=\\remoteserver\stuff\documents\
set dest=C:\Copied\
for /f "delims=" %%i in (dirlist.txt) do robocopy /s %directory%\%i% %dest%
echo.
echo Press any key to end...
pause> nul
In your “dirlist.txt” file, you can just specify the folder names relative to your %directory% variable. Just make sure you pay attention to your paths, spaces, and slashes in the paths.
If you’re just wanting to make an exact mirror of a directory (including deleting files & folders in the destination that aren’t in the source), check out the /MIR switch in the robocopy syntax (see link above).
I hope this helps! Let me know if you have any other questions. — oh, and I haven’t tested this idea, so you probably want to do a small scale test first.
Hi Brad,
Thank you for sharing this script. I was able to make it work for copying selected files from one source folder to one destination folder. I needed to copy from two source folders to one destination folder so I copied the commands and pasted them as a second set below the original set and placed the first source folder in the first set and the second source folder in the second set. It worked! However, my tweak is cumbersome to use in a situation where you need to copy a hundred source folders. I was wondering if you know of a better approach (without repeating the commands as many times as there are source folders).
Regards,
Joe
Hey Joe! Glad you stopped by and asked a question. I hope this helps.
From my understanding, you have multiple folders that have the same list of files you need to copy out of those folders. This list of files doesn’t include “every” file in these folders, so that’s why you’re specifying a smaller sub-set in your filelist.txt file. If I’ve misunderstood your problem, then my solution may not be what you need…
It sounds like you’re needing two “loops”. In my original example, the loop is the “for” part of the script. So in your case, let’s try a nested “for loop” like this :
@echo off
set directory=\\remoteserver\stuff\documents\
set dest=C:\Copied\
for /f "delims=" %%g in (srclist.txt) do (
for /f "delims=" %%i in (filelist.txt) do robocopy %directory%%%g %dest% "%%i"
)
echo.
echo Press any key to end...
pause> nul
A good way to test this would be to change “robocopy” in the loop to “echo”, then when you run it, you can make sure your paths are coming out like you want them to.
Let me know if you have any other questions!