Windows specific tutorials


Windows Batch Files
Windows Batch File Examples
Backing up using XCopy on Windows
 

Windows Batch Files

Using Batch Files is a very powerful and convenient 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 above Simple batch file code would launch notepad.

Passing Command line parameters to a batch file
This is done in the same way as for an executable file.  The parameters are entered after the command to launch 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.) that was 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) with:  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 booted this file up by clicking on its icon.  However, if you used another batch file located in a different directory to start up this batch file, then this code will not work.  You need to enter the full path:  DEL c:\junk\*.txt

Redirection of Output using > or >>
You can choose to redirect output from a command to a printer or to a file.
The command DIR *.txt > LPT1 - prints a list of .txt documents on the printer hooked up to port LPT1.
The command  DIR *.txt > c:\test.txt  saves a list of .txt documents to the file c:\test.txt
To append output to an existing file, use the >> sign. Ex: DIR *.txt >> c:\test.txt
 

Batch File Examples

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 Commandline

You need to enter the commandline arguments after the Filepath1.  A blank space is required after the Filepath1 and the first commandline argument.  A blank space is also required between commandline arguments if you need to specify more than one argument.

Useful  DOS Commands for Batch Files
From a MS-DOS window, type in ‘[command] /? | more’ for details on the following 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 very useful 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 all 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)