Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder named "C:\Jobs\job#1" , "C:\Jobs\job#2" "C:\Jobs\job#3" etc and a lot of directories and sub-directories under it. I want to get the all the directories under Jobs and xcopy them to C:\backup. Then I want to xcopy all the files under each Job#1, 2 ,3 etc. to C:\backup\job#1\month\\*.* To make it clearer.

Source dir = C:\Jobs\job#1\"myfiles&dir"

Destination  dir = C:\Backup\job#1\month\"myfiles&dir"

then do the next folder

Source dir = C:\Jobs\job#2\"myfiles&dir"

Destination dir = C:\Backup\job#2\month\"myfiles&dir"

...until all folders are back-up. Since the job folder keep increasing, by doing it this way I don't have to add extra code on this script except modify the month. Thank you.

share|improve this question

migrated from stackoverflow.com Nov 14 '11 at 8:25

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

If I understand you correctly, you're aiming to backup your folders recursively. I would suggest to use the /S parameter. Typehelp xcopy to get more information.

share|improve this answer
    
yes, but I don't know how to iterate each folder. I can only do the basic. –  user1044937 Nov 14 '11 at 23:06
add comment

Check that DATE /T on your computer gives it in this format and with the number part in the form MM/DD/YYYY That's a requirement for the 2 FOR statements to work. On my computer, DATE /T gives

C:\>DATE /T
Tue 11/15/2011

C:\>

Put a reference to the batch file in the task scheduler. you could run it once a month.. To test that the task scheduler is running it, I suggest moving your computer clock.. I've read that Task scheduler(possibly in XP) can be a bit funny with whether it executes batch files but test it.

Run this in task scheduler every month.

Contents of backup.bat for month\job format

FOR /F "TOKENS=1,2,3,4 DELIMS=/ " %%A IN ('DATE /T') DO @set mm=%%B
FOR /F "TOKENS=1,2,3,4 DELIMS=/ " %%A IN ('DATE /T') DO @set yyyy=%%D

xcopy c:\jobs c:\bkup\%yyyy%_%mm% /e /I

If you want it in job\month format, as you wrote, then firstly be aware that you given no place to put the files from c:\jobs you'd just be copying each subdirectory and its contents.

You only want this batch to run once a month, so put it in task scheduler.

For any of these xcopy lines that get run, if there is no file or subdirectory within the specified subdirectory of jobs, then no directory structure will be created, but that shouldn't matter.

Like if c:\jobs\a has no file and no directory in it. Then c:\bkup\a and c:\bkup\a\yyyy_mm will not be created. Which should be fine. Other jobs e.g. the job in subdirectory b, if there are any files or directories in it and there would be if you have any work there! it'd create the directories at the destination.

This may help you understand part of the backup.bat that follows

C:\>for /f %f in ('dir c:\jobs /ad/b') do @echo xcopy c:\jobs\%f c:\bkup\%f\%yyyy%_
%mm% /e /I
xcopy c:\jobs\a c:\bkup\a\2011_11 /e /I
xcopy c:\jobs\b c:\bkup\b\2011_11 /e /I
xcopy c:\jobs\c c:\bkup\c\2011_11 /e /I

Contents of backup.bat for backup in the job\month format.

FOR /F "TOKENS=1,2,3,4 DELIMS=/ " %%A IN ('DATE /T') DO @set mm=%%B
FOR /F "TOKENS=1,2,3,4 DELIMS=/ " %%A IN ('DATE /T') DO @set yyyy=%%D

for /f %f in ('dir c:\jobs /ad/b') do xcopy c:\jobs\%f c:\bkup\%f\%yyyy%_
%mm% /e /I
share|improve this answer
    
thanks barlop. Sorry for my vague question. Actually Job#1, Job#2 etc are just example. I want to back-up every folders under my jobs directory, folder name varies like "Crown Bakery" , "Mike's Repair Shop", " Dollar Laundry" etc.. The script will iterate on all folders under Jobs directory and copy all it's contents under C:\backup\Jobname\%month%\"back-up_files". So when I add another job folder under Jobs, it will do the back-up automaticaly without modifying the script. Thank you for you time and have a good day. –  user1044937 Nov 15 '11 at 4:19
    
@user1044937 I've done it for either job\month format, or month\job format. You choose from the answer which backup script you want to use. –  barlop Nov 16 '11 at 8:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.