?
Solved

How to copy sub dirs using batch file

Posted on 2005-05-10
6
Medium Priority
?
557 Views
Last Modified: 2008-02-01
Hi I'm new to batch files and I have this:

rem This is a template of a batch file that will backup to another drive :)
rem move to the D: drive of the machine
d:
rem move to the backup directory off the root
cd \mybackup\
rem THIS SHOULD BE MOVED TO ANOTHER DIR AND DELETED ONLY AFTER BACKING UP
re delete all files in this dir
delete *.*
rem delete all sub dirs in the dir
deltree*
rem create the new dirs
mkdir "mozilla"
mkdir "my z documents"
mkdir "d:\backup\my z documents\my pictures"
mkdir "d:\backup\my z documents\my webs"
rem copy the files into the backup directory
copy "Z:\My Documents\*.*" "d:\backup\my documents"
rem I WANT TO BACKUP ENTIRE DIRS and SUB DIRS WITHOUT HAVING TO NAME THEM ALL
copy "C:\Documents and Settings\myname\Application Data\Mozilla\thunderbird\*.*" "d:\backup\mozilla"
rem quit the program as we are finished the tack
exit
________

:) so where I have caps I need guidance.
1. I want to COPY and not DELETE the 'old' backup into a sub dir in case something goes wrong
2. When performing the actual back up, I don't want to have to name every dir.

Know what I mean? should be simple but I'm a total newbie :) thanks.
0
Comment
Question by:shantelechy
6 Comments
 
LVL 4

Author Comment

by:shantelechy
ID: 13971110
I meant to add... how can I make it run at a certain time each day :) thanks much
0
 
LVL 59

Assisted Solution

by:LeeTutor
LeeTutor earned 760 total points
ID: 13971182
You should use the XCOPY command rather than COPY, for it is much more powerful.  For example, it has the /e and /s switches to allow copying of subdirectories.  Here is the syntax for XCOPY:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\LeeTutor>xcopy /?
Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z]
                           [/EXCLUDE:file1[+file2][+file3]...]

  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  Each string
               should be in a separate line in the files.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  For
               example, specifying a string like \obj\ or .obj will exclude
               all files underneath the directory obj or all files with the
               .obj extension respectively.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /G           Allows the copying of encrypted files to destination that does
               not support encryption.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
0
 

Expert Comment

by:tripledukes
ID: 13972232
You can easily schedule it using windows taks scheduler.

Control Panel -> Scheduled Tasks -> add task
Navigate to the batch file, give it a schedule, give it appropriate credentials (so it can run no matter who is/isn't logged in), and finish creating.

The XCOPY Solution is a good one, but I personally prefer Robust file Copy (robocopy.exe).  It's a utility bundled into the Windows Resource Kit.  Download it here: http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

This utility will allow you to do continuous or incremental mirroring of a source and destination.  It's free and not terribly difficult, can backup ONLY CHANGED FILES every 30 seconds or every 30 days, and just runs continuously in the background.  Try it and let me know if you like it and I will give you further help if needed.  Oh by the way, it's totally free!!  Glood luck.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Accepted Solution

by:
craylord earned 840 total points
ID: 13979459
Put the following into a batch file. It will copy your desktop, favorites, my documents, and the thunderbird info to the d share. It will put them into a mybackup folder, if the mybackup folder already exists it will rename it to mypreviousbackup folder. This will take the existing backup and rename it before copying again. Renaming the folder will overwrite the previous backup so don't worry about having tons of folders. Just your main backup folder and a folder containing the previous backup. Not too sure about the line - copy "Z:\My Documents\*.*" "d:\backup\my documents"? Typo? or do you have a z:drive?


ren d:\mybackup d:\mypreviousbackup
xcopy "%userprofile%\Desktop\*" "d:\mybackup\desktop" /E /H /C /D /Y
xcopy "%userprofile%\Favorites\*" "d:\mybackup\favorites" /E /H /C /D /Y
xcopy "%userprofile%\My Documents\*" "d:\mybackup\my documents\" /E /H /C /D /Y
xcopy "%userprofile%\Application Data\Mozilla\thunderbird\*" "d:\backup\mozilla\" /E /H /C /D /Y
0
 
LVL 16

Expert Comment

by:craylord
ID: 13979468
xcopy "%userprofile%\Application Data\Mozilla\thunderbird\*" "d:\backup\mozilla\" /E /H /C /D /Y

should be

xcopy "%userprofile%\Application Data\Mozilla\thunderbird\*" "d:\mybackup\mozilla\" /E /H /C /D /Y
0
 
LVL 4

Author Comment

by:shantelechy
ID: 13981803
I have another hard drive called z drive.

Craylord, your answer expands in the way I need on the useful learning info that Leetutor provided. Thank you ALL, very helpful.
Now I must find out to split points
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 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