?
Solved

Batch file to copy a folder multiple times

Posted on 2011-05-04
6
Medium Priority
?
653 Views
Last Modified: 2012-06-27
I have to create hundreds of copies of a single folder and contents, each copy top level folder needs renaming to a list of name contained in a .txt each name on a new line

is there a way to do this using a .bat

 
0
Comment
Question by:QuinnDester
  • 4
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35688636
Sure.  Try this:

@echo off
set sourcedir=D:\sourcedir
set destbasedir=D:\newbasedir
set textlist="D:\newbasedir\textlist.txt"
for /f %%a in ('%textlist%') do (
  md "%destbasedir%\%%a" 2>NUL
  xcopy /d /e "%sourcedir%\*.*" "%destbasedir%\%%a"
)

Amend the three set lines to point to the dir you want copying, where you want them put, and the text file containing the subdir names in textlits.txt.
It will run down each line in the textlist.txt file and for each create a dir by the name on that line under the destination directory and then run an xcopy command from the source to this dir.  At the moment it uses /d and /e to copy only newer files if this also ends up refreshing existing directories and /e to copy subdirectories too which you can remove if not needed.

hth

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35688647
Sorry I should have said this assumes the directory names don't have spaces in.  If they do try this amendment:

@echo off
set sourcedir=D:\sourcedir
set destbasedir=D:\newbasedir
set textlist="D:\newbasedir\textlist.txt"
for /f "tokens=* delims=" %%a in ('%textlist%') do (
  md "%destbasedir%\%%a" 2>NUL
  xcopy /d /e "%sourcedir%\*.*" "%destbasedir%\%%a"
)
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 35688745
Thanks, folder names do not contain spaces so have used your first example

@echo off
set sourcedir=C:\Documents and Settings\Chris\My Documents\foldercreate test\Simply-Cashback
set destbasedir=C:\Documents and Settings\Chris\My Documents\foldercreate test
set textlist="C:\Documents and Settings\Chris\My Documents\foldercreate test\guesthouselist.txt"
for /f %%a in ('%textlist%') do (
  md "%destbasedir%\%%a" 2>NUL
  xcopy /d /e "%sourcedir%\*.*" "%destbasedir%\%%a"
)

however this doesnt work, it opens the text file and just sits there doing nothing, have i edited your example correctly?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 35688825
Sorry, my bad.  Add the word "type" into the for command as i had used ' ' to take output of a program (we can do other ways but that will work).

Here is my amended code and my example run 1 and 2 of it:

C:\27017061>test
C:\27017061\sourcedir\test.cmd -> C:\27017061\test dest dir\test1\test.cmd
1 File(s) copied
C:\27017061\sourcedir\test.cmd -> C:\27017061\test dest dir\test2\test.cmd
1 File(s) copied
C:\27017061\sourcedir\test.cmd -> C:\27017061\test dest dir\test3\test.cmd
1 File(s) copied
C:\27017061\sourcedir\test.cmd -> C:\27017061\test dest dir\test4\test.cmd
1 File(s) copied

C:\27017061>test
0 File(s) copied
0 File(s) copied
0 File(s) copied
0 File(s) copied

@echo off

REM Set variables
set sourcedir=C:\Documents and Settings\Chris\My Documents\foldercreate test\Simply-Cashback
set destbasedir=C:\Documents and Settings\Chris\My Documents\foldercreate test
set textlist="C:\Documents and Settings\Chris\My Documents\foldercreate test\guesthouselist.txt"

REM create basedir if not present
md "%destbasedir%" 2>NUL

REM work down each line in textlist creating any missing dirs then copying the source files in there
for /f %%a in ('type %textlist%') do (
  md "%destbasedir%\%%a" 2>NUL
  xcopy /d /e /f "%sourcedir%\*.*" "%destbasedir%\%%a"
)
0
 
LVL 3

Author Closing Comment

by:QuinnDester
ID: 35688939
excellent thank you, its complete and you have probably saved me 3 days of work, and no risk of human error :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35689034
No problem, glad it helped!

Steve
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question