Windows specific tutorials
Windows Batch Files
Windows Batch File Examples
Backing up using XCopy on Windows
Using Batch Files is a very powerful way to enhance your productivity.
Batch files are simple text files with commands.
Each command has to be entered on a new line followed by a carriage return.
The Text file is saved as ‘xxxx.bat’ to generate a batch file. . The maximum length of the filename is 8 characters.
Create a Simple Batch file
Open up a new text document using notepad.
Type in the Following command on the first line: Start notepad
From the ‘File Menu’ select ‘Save As’
In the File Save Dialog Box, select ‘All Files (*.*)’ as the File Type.
Enter the name of the batch file to save as. Ex: Test.bat
Running a Batch file
To run a batch file, double click on its icon. For example, the Simple batch file code above, would launch notepad.
Passing Command-line parameters to a batch file
The parameters are entered after the full path to the batch file. There is a space between parameters.
Example: c:\mybat.bat command1 command2 command3
Using Command-line parameters in a batch file
Within the batch file, use %1 to refer to the first command line parameter (%2 for the second parameter etc.). This parameter is passed while launching the batch file.
Ex: To launch a batch file, and tell it to copy the file "c:\test.txt" to drive a:, we would do the following:
1) Launch your batch file(c:\mybat.bat) using: c:\mybat.bat c:\test.txt
2) In mybat.bat, use the following code: Copy %1 a:
Concept of Start/Current directory
If you plan to use relative paths in your batch file, it should know its current directory.
For example, to delete all text files in the current directory, we would use a batch file with the following code: DEL *.txt
This would work, if you run the batch file, by clicking on its icon.
However, if you used another batch file, located in a different directory, to run this batch file, then this code will not work. You need to enter the full path: DEL c:\junk\*.txt
In the examples below, Filepath1, Filepath2 refer to the full path of the executable files.
Ex: c:\program files\programs.exe or c:\windows\notepad.exe
The syntax for the Start command varies between Win95 and WinNT families.
Example 1: Starting programs simultaneously
Start Filepath1
Start Filepath2
Example 2: Starting 2nd program after 1st finishes
Start /w Filepath1
Start /w Filepath2
The /w switch tells the windows to wait for the programs to finish.
Example 3: Running program with command-line arguments
Start /w Filepath1 Command-line
You need to enter the command-line arguments after the Filepath1. A blank space is required after the Filepath1 and the first command-line argument. A blank space is also required between command-line arguments if you need to specify more than one argument.
Useful DOS Commands for Batch Files
From a MS-DOS window, type in 'command /?' for details on the commands. Some of these commands may not be available to you, depending on your system. For some commands like Ftp, type ‘ftp’ in a dos window. At the subsequent prompt, type ‘help’.
Commands which may be of use to you
attrib, break, call, command, copy, chcp, chdir (cd), chkdsk, cls, date, debug, del, deltree, dir, diskcopy, doskey, defrag, drvspace, dir, echo, edit, erase, exit, expand, fc, find, for, format, fdisk, ftp, goto, if, label, mem, mkdir (md), mode, more, move, netstat, net, path, pause, ping, prompt, rem, rename (ren), rmdir (rd), set, shift, sort, start, subst, time, type, verify, vol, xcopy
Backing up using XCopy
MS-DOS comes with a xcopy utility, which you can use, to backup files, or directories. The exact syntax varies with operating systems. Win 95, Win NT, and Win 2000 pro, have different syntax for XCopy. From a command window use xcopy /? for more information.
For example, to backup the c:\junk directory and all its sub directories to a:, and replace older files with newer files, and overwrite existing files without prompting, use
xcopy c:\junk a: /D /E /Y (the /Y switch may or may not be used depending on your system)