• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Email Me
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
madbray.com

madbray.com

The WP Experiment

  • Home
  • Blog
  • Shop
  • Contact
  • About

Copy Files From Multiple Folders Using Robocopy and a Text File List

April 25, 2018 by Brad May 4 Comments

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.

Share this:

  • Twitter
  • Facebook

Reader Interactions

Comments

  1. Doug says

    February 12, 2021 at 9:54 am

    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

    Reply
    • Brad May says

      February 20, 2021 at 1:29 pm

      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.

      Reply
  2. Joe says

    February 15, 2021 at 11:51 pm

    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

    Reply
    • Brad May says

      February 21, 2021 at 12:16 am

      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!

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Join the WP Experiment

Top Rated Stuff

  • madbray.com coffee mug Coffee Mug - The Official Coffee Vessel $15.80
  • hand me a burrito t-shirt "hand me a burrito" t-shirt $27.40
  • Another Official Coffee Vessel $18.10

Connect With Me

  • Email
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Recent Posts

  • Best YouVersion Reading Plan for Entrepreneurs
  • My Real World Experience with Tayst Coffee Pods
  • How to See CHKDSK Results in Event Viewer
  • How to Turn Off AT&T DNS Error Assist
  • Android 12 Update: How to Choose Browser to Open Links

Footer

Popular Posts

  • Best YouVersion Reading Plan for Entrepreneurs
  • My Real World Experience with Tayst Coffee Pods
  • How to See CHKDSK Results in Event Viewer
  • How to Turn Off AT&T DNS Error Assist
  • Android 12 Update: How to Choose Browser to Open Links

Tags

access denied Android asterisk at&t backup beach branch change chan_sip cisco 7960 clickbank cmd coffee command prompt database electric guitar encryption Essentialism firefox freepbx genesis google docs Greg McKeown growth internet mailchimp mysql online business passwords pjsip plugins remote desktop res_pjsip review security sip siteground sunrise survey monkey veeam web hosting Windows WordPress WordPress development worship practice

Topics

  • Coffee (1)
  • Faith (8)
    • Bible Verses (1)
  • Musings (8)
  • Online Business (8)
  • Projects (4)
  • Technology (20)
  • Uncategorized (1)
  • WordPress (6)
    • Development (1)

Copyright © 2023 · Kreativ Pro On Genesis Framework · Proudly running on WordPress