Batch Programming .
Scenario: Copy Files with xcopy
Simple copy tasks can be done with batch files. Usually, copy tasks are realized with xcopy which is an advanced version of copy. For incremental copies however it is recommended to use robocopy instead of xcopy. Robocopy is not included in XP but can be downloaded as part of MS research kit. (Its included in Vista!). For options or possible parameters please open a command prompt and type xcopy /?. Im not going to dwell on all these parameters since you can just see them on the prompt. Just a simple example: We want to copy a bunch of .doc-files (Word files) from one folder to a USB flash drive. Only doc-files should be copied and only those files that were altered on the 12th of december 2009 and no other files. Moreover, we want a confirmation before the copy process even starts. The script looks like this:
@echo off xcopy D:\documents\*.doc K:\documents\ /s /p /f /d:12-12-2009 pause Note here: K:\ or whatever letter it is, is your flash drive or your USB hard disk. /P expects a confirmation and an user input when a file is about to be overwritten. *.doc stands for every doc-file, all file hat have the ending .doc. You can also do this with txt files so *.txt or *.odt, if you have OpenOffice. Now this can be extended with more parameters. So, for example, there are files that need to be excluded. We dont want to copy them. This can be done with the option /exclude: @echo off xcopy D:\documents\*.doc K:\documents\ /s /p /f /d:12-12-2009 /exclude:d:exclude.txt pause
Now for this, you need to create a txt-file with your editor. Open it and type the names that you don t want to copy. Each filename has to be in a new line, so like: doc1 doc2 doc3 Give it a name and put the name like it is shown in the code above. So /exclude:pathtothefileandname.txt
You can even exclude different type of files. If you put *.odt in the txt file, just *.odt in the first line and all *.odt files will be excluded.
Now, you can also get files from a client in your LAN. If that client has got a shared folder and you want to get the files you just use xcopy to get them. Make sure you write the correct syntax for this: xcopy \\192.168.2.105\cd1\example.txt d:\ Here, 192.168.2.105 is just an example, so put the correct IP-address in there. Cd1 is the folder that is shared among the clients and example.txt is the file you want to copy. D:\ is the files destination. Xcopy \\192.168.2.105\cd1\*.* d:\ Here, *.* acts as a substitute for all files in the folder cd1. Note: Sometimes it can be useful to copy the rights and ACLs for files. Use the option /O for this.