PDQ Library:  Batch Program to Copy Files

You must already know how to use batch commands to use this page. I accept
no responsibility for any accidents you have using batch commands.

You can create a Batch program in a Windows system to back up your personal files easily and quickly. This is an alternative to running the Windows Backup program in continuous mode. Microsoft Windows includes a "batch" command language with an XCOPY command that you can use to copy files such as your email, address books, and bookmarks that change daily. Then create a "shortcut" of the .bat file on your Desktop. You can even run it automatically once a day using the Windows Task Scheduler.

If you are using Windows 7, you can backup entire folders using ROBOCOPY instead of this method.

Creating a Batch program:

You must edit all file paths in the sample batch program to match the location of your particular files.

copyfiles.txt

  1. Save and edit our sample program to create your own. First, right click the link above and "save link as" the same name.
  2. Open the file in Notepad or other plain text editor. Long lines must NOT be split into two!
  3. Edit text G:\ to match your backup disk letter (see My Computer to view your devices).
  4. Edit LOGIN to your Login name (i.e. Nancy or Administrator).
  5. Edit all XCOPY command lines. To illustrate, I copied typical Thunderbird email account files. First the main email file inbox is copied. Then all Thunderbird address books (file names start with abook..).
  6. Remove :: (comments) from the beginning of XCOPY lines after editing them to copy your own mail files that you want to back up frequently.
  7. XCOPY command is controlled by switch codes.
    • /P a confirmation switch will prompt you to type "Y" (followed by the Enter key) before each copy operation.
    • /M switch copies only files with the archive bit set, then clears it so that only new or modified files are copied each time the COPYFILES batch program is run.
    • /Y switch works with the /M switch to suppress confirmation overwriting an existing (older) file.
    • Note: it is important to include the second slash in G:\Mail\ to indicate that you want to copy to a SubFolder (rather than a file called Mail). If the subfolder does not exist, it will be created automatically.
  8. Save your edited commands to a file called COPYFILES.BAT.
  9. Copy COPYFILES.BAT to the formatted removable disk that will receive your backup files (Zip, CD, or floppy). The first command checks for the file as a safeguard against using the wrong disk.

Test your program:

Run COPYFILES.BAT and test each copy operation. Once the program is tested several times with updated files, you may delete the confirmation prompt /P in command lines.

A file called COPY.TXT is created in the same folder as COPYFILES.BAT to log the files that are copied. A new log file is created using >COPY.TXT (and appends to that file using >>COPY.TXT). This log file is overwritten each time you run COPYFILES.BAT and can help you identify typos or other problems.

Batch program to Copy your important Folders (Windows 7)

Windows 7 has a very useful tool called "ROBOCOPY" that can backup all the files in an entire folder. It uses the same techniques as XCOPY with switches to control the way the copy is carried out. Here is an example - you must edit USERNAME to your user name. Or add any other folders you wish to backup. Notice that the target folder and the command line options are put into environment variables, which makes it easy to add new commands. To see all the command line options, open up a "Command Line" windows (or run "%windir%\system32\cmd.exe") and type "ROBOCOPY /?". If you want to save the output to a file, type "ROBOCOPY /? > C:\Users\USERNAME\Documents\ROBOCOPY-info.txt".

copyfiles-win7.txt

How to Zip up every file in a folder

For 7-Zip fans, here is a batch program that will zip all files in "My Documents" and creates the file Backup_08-02-10.zip using today's date (date format depends on your computer settings). You may edit the path after "CD", and the file wildcard * can be changed to any file type (*.htm*, *.xls, *.log). Get 7-ZIp in our Download page.

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set Date=%%i )
REM Edit the path below to any folder:
CD "C:\Documents and Settings\%username%\My Documents"
"C:\Program Files\7-Zip\7zFM.exe" a -tzip Backup_%Date% *
TOP back