Wednesday, January 7, 2009

How to use the xcopy dos command in a batch file in Windows XP

Xcopy is a dos command that can copy files and directory trees. If used in a batch file the xcopy command will copy a sourse file of your choice and put it in the destination of your choice. The xcopy command has a number of switches that you can use manipulate what the xcopy command does. A switch is a code, or parameter, that modifies a command

How to find out what switches you can use with xcopy?

Press on the WIN KEY and the letter R to open a run box
Then type cmd and then press OK
Now type xcopy/?
The following information about will come up

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.

How to use xcopy command in a batch file?

If you simply want to copy one file to another place then you can type this into a notepad file:
xcopy "C:\Documents and Settings\Yourusername\My Documents\My Pictures" "H:\backup of pictures" /e /y
This is a basic xcopy command that is telling the batch file to copy the contents of my pictures from C: drive and put it in H:\backup of pictures. The /e is needed to copy all the directories and sub directories from the my pictures folder and the /y is needed so it will just copy over already exsisting files instead of asking to overwrite the files.
You can use other switches to make it do other things.

Notes:

  • Make sure you have a space after the xcopy command, a space between the sourse and destination, and a space after the destination to start the switches, and a space between the swiotches. I know this sounds a bit stupid, however if you have two spaces or no spaces the code will be invalid.

How to create a simple batch file to backup files or folders

How to create a simple batch file to backup files or folders

Creating a simple batch file to backup files or folders is a cool way to move, copy, and backup any file you like.

A batch file is a text file saved as a .bat extension and when doubled clicked on it will execute the dos commands contained in that file.

In this article I will be using the xcopy command and the robocopy command to create a batch file to copy a file or folder. If you have Windows XP you will use xcopy for your batch file and if you have Windows Vista you can use the new command, robocopy.


First we need to create a batch file:


  1. Open Notepad by going to the start menu>all programs>accessoires>then choose Notepad.

  2. Type your commands into the notepad file.

  3. Then go to the file menu at the top and choose Save As.

  4. Now save the file as Yourfilename.bat

Some examples on what to write in a batch file to copy or backup files.

xcopy "C:\Documents and Settings\Your username\My Documents\My Pictures" "H:\backup of pictures" /e /y

This command will backup my pictures on C: drive and paste it to H: drive in a folder called backup of pictures. If you put the destination as just H: than all the files will be all over the place. You must specify a folder. I usually make a new folder first. Of course you can also get the batch file to make the new folder for you but this is another article altogether.

/e means to copy all directories and sub directories

/y means to copy with prompting to overwrite the already exsisting files

See this article on How to use xcopy dos command and switches in batch file