How to copy sub dirs using batch file

Posted on 2005-05-10
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
rem move to the backup directory off the root
cd \mybackup\
re delete all files in this dir
delete *.*
rem delete all sub dirs in the dir
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"
copy "C:\Documents and Settings\myname\Application Data\Mozilla\thunderbird\*.*" "d:\backup\mozilla"
rem quit the program as we are finished the tack

:) 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.
Question by:shantelechy
    LVL 4

    Author Comment

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

    Assisted Solution

    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]

      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.
                   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.

    Expert Comment

    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:

    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.
    LVL 16

    Accepted Solution

    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
    LVL 16

    Expert Comment

    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
    LVL 4

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Can you find a fax from a vendor you saved a decade ago in seconds? Have you ever cursed your PC under your breath during an audit because you couldn’t find the requested statement or driver history?  If you answered no to the first question or yes …
    Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now