Commands and Batch Files

Overview

Some obvious questions: Windows traditionally came with a very rudimentary way to automate some tasks. The technique is batch file programming. Newer version of Windows are beginning to include more advanced tools such as power shell.

In its most simplest form, a batch file is simply a text file that contains a bunch of commands to run. Hence the term "batch". For example, the following shows the contents of a very trivial batch file that:

Batch file with three commands:
:: Builds, runs, and diffs the test program
gcc -o program.exe driver.c file.c 
program.exe > output.txt
diff output.txt output-master.txt
If the name of this batch file is build.bat, then you simply type that filename on the command line:
C:\> build.bat
Like executables, supplying the extension is optional (and rarely needed):
C:\> build
Essentially, if you can type it on the command line, you can put it in a batch file.

Commands

The command interpreter on Windows is cmd.exe. (Older versions used command.exe.) When you type a command in the command window, the system is searched in the order listed below. As soon as a matching command is found, the search ends.
  1. Internal commands are looked at first
  2. External commands are searched:
    1. The current directory
    2. Directories listed in the PATH environment variable are searched in the order listed
If no extension is provided, the search appends these extensions in order:
  1. .com
  2. .exe
  3. .bat
  4. .cmd
So, if you have a file named go.exe and go.bat and you type just go at the command line, then go.exe will run. If you want to run the batch file, you must type go.bat to override the default behavior.

All commands have the same structure and syntax. Once you understand this simple syntax, the commands won't appear so cryptic and arbitrary. The structure of a command is:

COMMAND [/option1 /option2 /option3 ...] [arg1 arg2 arg3 ...]
Some Windows commands will accept the minus sign - as well as the forward slash / (Linux and Mac OS X don't use the forward slash):
COMMAND [-option1 -option2 -option3 ...] [arg1 arg2 arg3 ...]
Also, some options can take arguments themselves:
COMMAND [-option1 optarg1 -option2 -option3 optarg3 ...] [arg1 arg2 arg3 ...]
Notes: (These apply to commands on most operating systems, including Windows, Linux, and Mac OS X) Examples: Try all of these on a Windows computer. (You may have to modify the directory names)


Redirection and Piping

This is where the real power of the command line lies. It is simply impossible (or, at the very least, tremendously difficult) to do these things using a graphical user interface (GUI). There is a wealth of information about this on the Internet. You can start here and here to learn more about this for Windows.

Fortunately, Windows does have the ability to do redirection and piping, but, unfortunately, Windows does not provide most of the necessary commands/programs to make use of these techniques. The easiest way to get these missing commands/programs is to install Cygwin on Windows. That will give you 95% of what you will need to be productive from the command line in Windows. For Linux and Mac OS X, all of the necessary commands/programs are already present.

In a nutshell:

Examples showing commands, redirection, piping, switches, arguments: (sort, cut, and uniq are part of Cygwin, not Windows)

C:\> ipconfig | grep "IP Address" | cut -c 45- | grep "192.168" | sort

C:\> ipconfig | grep "IP Address" | cut -c 45- | grep "192.168" | cut -b9- | sort

C:\> ipconfig | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort | uniq
C:\> ipconfig > ip.txt
C:\> grep "IP Address" < ip.txt | cut -c 45- | cut -d. -f1,2 | sort | uniq > results.txt
C:\> type results.txt
Here is ip.txt from a Windows XP computer. You'll have to modify the command above for Windows 7 because the format of the output has changed.

More info.

Detailed info on the Windows NT command shell. By the way, Windows NTWindows NT is the basis for all NT operating systems (2000, Vista, 7, etc.) Previous versions of Windows (Windows 3.0, Windows 95, 98) were different (internally). And, yes, Windows NT really did start at version 3.1!

Marketing NameInternal NameDate ReleasedBuild No.
Windows NT 3.1 NT 3.1 July 1993 528
Windows NT 3.5 NT 3.5 September 1994807
Windows NT 3.51 NT 3.51May 1995 1057
Windows NT 4 NT 4.0 July 1996 1381
Windows 2000 NT 5.0 December 1999 2195
Windows XP NT 5.1 August 2001 2600
Windows Server 2003 NT 5.2 March 2003 3790
Windows Vista NT 6.0 January 2007 6000
Windows Server 2008 NT 6.0 March 2008 6001
Windows 7 NT 6.1 October 2009 7600
Windows Server 2008 R2NT 6.1 October 2009 7600
Windows 8 NT 6.2 October 2012 9200
Windows Server 2012 NT 6.2 September 20129200

Short Reference

There are many commands used in batch file programming, and like most programming, they follow the 80/20 rule (or even 90/10) rule:
"80% of the time you will use 20% of the commands"
The fastest way to find out how a command works is to get help from the command line. Windows doesn't have a facility like the man pages on Linux and with Cygwin. Instead, to get help on a command type:
C:> command /?
or
C:> help command
where command is an internal or external command (e.g. dir, copy, attrib, etc.)

In my opinion, these are the commands you'll use most of the time. Consider them the "core" commands. Well, some aren't really core, but are simple and you may find them useful on their own or within batch files.

Some core batch file techniques
CommandBrief Description
attrib.exe[external] Read/set file attributes (details)
callCall a batch file from within a batch file (details)
chdir, cdChange directory (details)
choicePrompt user for input. Not in XP or before. (details)
clsClears the screen (details)
colorChanges command window colors (details)
copyCopy files (details)
dateRead or set the date (details)
delDelete files (details)
dirList files (details)
echoPrint to the screen (details)
forLooping construct (details)
gotoJump to a label in a batch file (details)
ifConditional construct (details)
mkdir, mdCreate (make) a new directory (details)
moveMove files (details)
pathDisplay the value of the PATH variable (details)
pausePause a batch file (details)
promptSet the text/style of the prompt (details)
popdPop most recent directory and change to it (details)
pushdSave current directory and change (details)
rmdir, rdDeletes (removes) a directory (details)
remComment (remark), also :: (details)
rename, renRename a file (details)
setRead or set an environment variable (details)
shiftCycle through parameters to batch files (details)
startStart another program (details)
typeDisplay the contents of a text file (details)
xcopy.exe[external] A more powerful way to copy files (details)

One of the most important aspects of working at the command prompt is to customize it so you can be productive. This includes things like:

Long Reference

By the way, the bulk of this reference was created with a batch file called makehelp.bat. Modifying it so that there are hyperlinks and matches the output below is an exercise for the student. The resulting HTML output will look like this: winhelp.html.

Hints:


ATTRIB Displays or changes file attributes. ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [drive:][path][filename] [/S [/D]] + Sets an attribute. - Clears an attribute. R Read-only file attribute. A Archive file attribute. S System file attribute. H Hidden file attribute. [drive:][path][filename] Specifies a file or files for attrib to process. /S Processes matching files in the current folder and all subfolders. /D Processes folders as well. Back to table


CALL Calls one batch program from another. CALL [drive:][path]filename [batch-parameters] batch-parameters Specifies any command-line information required by the batch program. If Command Extensions are enabled CALL changes as follows: CALL command now accepts labels as the target of the CALL. The syntax is: CALL :label arguments A new batch file context is created with the specified arguments and control is passed to the statement after the label specified. You must "exit" twice by reaching the end of the batch script file twice. The first time you read the end, control will return to just after the CALL statement. The second time will exit the batch script. Type GOTO /? for a description of the GOTO :EOF extension that will allow you to "return" from a batch script. In addition, expansion of batch script argument references (%0, %1, etc.) have been changed as follows: %* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...) Substitution of batch parameters (%n) has been enhanced. You can now use the following optional syntax: %~1 - expands %1 removing any surrounding quotes (") %~f1 - expands %1 to a fully qualified path name %~d1 - expands %1 to a drive letter only %~p1 - expands %1 to a path only %~n1 - expands %1 to a file name only %~x1 - expands %1 to a file extension only %~s1 - expanded path contains short names only %~a1 - expands %1 to file attributes %~t1 - expands %1 to date/time of file %~z1 - expands %1 to size of file %~$PATH:1 - searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string The modifiers can be combined to get compound results: %~dp1 - expands %1 to a drive letter and path only %~nx1 - expands %1 to a file name and extension only %~dp$PATH:1 - searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found. %~ftza1 - expands %1 to a DIR like output line In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid argument number. The %~ modifiers may not be used with %* Back to table


CHDIR Displays the name of or changes the current directory. CHDIR [/D] [drive:][path] CHDIR [..] CD [/D] [drive:][path] CD [..] .. Specifies that you want to change to the parent directory. Type CD drive: to display the current directory in the specified drive. Type CD without parameters to display the current drive and directory. Use the /D switch to change current drive in addition to changing current directory for a drive. If Command Extensions are enabled CHDIR changes as follows: The current directory string is converted to use the same case as the on disk names. So CD C:\TEMP would actually set the current directory to C:\Temp if that is the case on disk. CHDIR command does not treat spaces as delimiters, so it is possible to CD into a subdirectory name that contains a space without surrounding the name with quotes. For example: cd \winnt\profiles\username\programs\start menu is the same as: cd "\winnt\profiles\username\programs\start menu" which is what you would have to type if extensions were disabled. Back to table


CLS Clears the screen. CLS Back to table


COLOR Sets the default console foreground and background colors. COLOR [attr] attr Specifies color attribute of console output Color attributes are specified by TWO hex digits -- the first corresponds to the background; the second the foreground. Each digit can be any of the following values: 0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White If no argument is given, this command restores the color to what it was when CMD.EXE started. This value either comes from the current console window, the /T command line switch or from the DefaultColor registry value. The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same. Example: "COLOR fc" produces light red on bright white Back to table


COPY Copies one or more files to another location. COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]] source Specifies the file or files to be copied. /A Indicates an ASCII text file. /B Indicates a binary file. /D Allow the destination file to be created decrypted destination Specifies the directory and/or filename for the new file(s). /V Verifies that new files are written correctly. /N Uses short filename, if available, when copying a file with a non-8dot3 name. /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. Default is to prompt on overwrites unless COPY command is being executed from within a batch script. To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format). Back to table


DATE Displays or sets the date. DATE [/T | date] Type DATE without parameters to display the current date setting and a prompt for a new one. Press ENTER to keep the same date. If Command Extensions are enabled the DATE command supports the /T switch which tells the command to just output the current date, without prompting for a new date. Back to table


DEL Deletes one or more files. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names names Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted. /P Prompts for confirmation before deleting each file. /F Force deleting of read-only files. /S Delete specified files from all subdirectories. /Q Quiet mode, do not ask if ok to delete on global wildcard /A Selects files to delete based on attributes attributes R Read-only files S System files H Hidden files A Files ready for archiving - Prefix meaning not If Command Extensions are enabled DEL and ERASE change as follows: The display semantics of the /S switch are reversed in that it shows you only the files that are deleted, not the ones it could not find. Back to table


DIR Displays a list of files and subdirectories in a directory. DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4] [drive:][path][filename] Specifies drive, directory, and/or files to list. /A Displays files with specified attributes. attributes D Directories R Read-only files H Hidden files A Files ready for archiving S System files - Prefix meaning not /B Uses bare format (no heading information or summary). /C Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator. /D Same as wide but files are list sorted by column. /L Uses lowercase. /N New long list format where filenames are on the far right. /O List by files in sorted order. sortorder N By name (alphabetic) S By size (smallest first) E By extension (alphabetic) D By date/time (oldest first) G Group directories first - Prefix to reverse order /P Pauses after each screenful of information. /Q Display the owner of the file. /S Displays files in specified directory and all subdirectories. /T Controls which time field displayed or used for sorting timefield C Creation A Last Access W Last Written /W Uses wide list format. /X This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place. /4 Displays four-digit years Switches may be preset in the DIRCMD environment variable. Override preset switches by prefixing any switch with - (hyphen)--for example, /-W. Back to table


ECHO Displays messages, or turns command-echoing on or off. ECHO [ON | OFF] ECHO [message] Type ECHO without parameters to display the current echo setting. Back to table


FOR Runs a specified command for each file in a set of files. FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file. command-parameters Specifies parameters or switches for the specified command. To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I. If Command Extensions are enabled, the following additional forms of the FOR command are supported: FOR /D %variable IN (set) DO command [command-parameters] If set contains wildcards, then specifies to match against directory names instead of file names. FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters] Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree. FOR /L %variable IN (start,step,end) DO command [command-parameters] The set is a sequence of numbers from start to end, by step amount. So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would generate the sequence (5 4 3 2 1) FOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ("string") DO command [command-parameters] FOR /F ["options"] %variable IN ('command') DO command [command-parameters] or, if usebackq option present: FOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ('string') DO command [command-parameters] FOR /F ["options"] %variable IN (`command`) DO command [command-parameters] filenameset is one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens. The body of the for loop is then called with the variable value(s) set to the found token string(s). By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. You can override the default parsing behavior by specifying the optional "options" parameter. This is a quoted string which contains one or more keywords to specify different parsing options. The keywords are: eol=c - specifies an end of line comment character (just one) skip=n - specifies the number of lines to skip at the beginning of the file. delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab. tokens=x,y,m-n - specifies which tokens from each line are to be passed to the for body for each iteration. This will cause additional variable names to be allocated. The m-n form is a range, specifying the mth through the nth tokens. If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. usebackq - specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset. Some examples might help: FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse. %i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'. Remember, FOR variables are single-letter, case sensitive, global, and you can't have more than 52 total active at any one time. You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string, using single quote characters. It will be treated as a single line of input from a file and parsed. Finally, you can use the FOR /F command to parse the output of a command. You do this by making the filenameset between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file. So the following example: FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i would enumerate the environment variable names in the current environment. In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax: %~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file %~$PATH:I - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string The modifiers can be combined to get compound results: %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found. %~ftzaI - expands %I to a DIR like output line In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive. Back to table


GOTO Directs cmd.exe to a labeled line in a batch program. GOTO label label Specifies a text string used in the batch program as a label. You type a label on a line by itself, beginning with a colon. If Command Extensions are enabled GOTO changes as follows: GOTO command now accepts a target label of :EOF which transfers control to the end of the current batch script file. This is an easy way to exit a batch script file without defining a label. Type CALL /? for a description of extensions to the CALL command that make this feature useful. Back to table IF Performs conditional processing in batch programs. IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT Specifies that Windows should carry out the command only if the condition is false. ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified. string1==string2 Specifies a true condition if the specified text strings match. EXIST filename Specifies a true condition if the specified filename exists. command Specifies the command to carry out if the condition is met. Command can be followed by ELSE command which will execute the command after the ELSE keyword if the specified condition is FALSE The ELSE clause must occur on the same line as the command after the IF. For example: IF EXIST filename. ( del filename. ) ELSE ( echo filename. missing. ) The following would NOT work because the del command needs to be terminated by a newline: IF EXIST filename. del filename. ELSE echo filename. missing Nor would the following work, since the ELSE command must be on the same line as the end of the IF command: IF EXIST filename. del filename. ELSE echo filename. missing The following would work if you want it all on one line: IF EXIST filename. (del filename.) ELSE echo filename. missing If Command Extensions are enabled IF changes as follows: IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command where compare-op may be one of: EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed. The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is comparing against an internal version number associated with the Command Extensions. The first version is 1. It will be incremented by one when significant enhancements are added to the Command Extensions. CMDEXTVERSION conditional is never true when Command Extensions are disabled. The DEFINED conditional works just like EXISTS except it takes an environment variable name and returns true if the environment variable is defined. %ERRORLEVEL% will expand into a string representation of the current value of ERRORLEVEL, provided that there is not already an environment variable with the name ERRORLEVEL, in which case you will get its value instead. After running a program, the following illustrates ERRORLEVEL use: goto answer%ERRORLEVEL% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 You can also using the numerical comparisons above: IF %ERRORLEVEL% LEQ 1 goto okay %CMDCMDLINE% will expand into the original command line passed to CMD.EXE prior to any processing by CMD.EXE, provided that there is not already an environment variable with the name CMDCMDLINE, in which case you will get its value instead. %CMDEXTVERSION% will expand into a string representation of the current value of CMDEXTVERSION, provided that there is not already an environment variable with the name CMDEXTVERSION, in which case you will get its value instead.


MKDIR Creates a directory. MKDIR [drive:]path MD [drive:]path If Command Extensions are enabled MKDIR changes as follows: MKDIR creates any intermediate directories in the path, if needed. For example, assume \a does not exist then: mkdir \a\b\c\d is the same as: mkdir \a chdir \a mkdir b chdir b mkdir c chdir c mkdir d which is what you would have to type if extensions were disabled. Back to table


MOVE Moves files and renames files and directories. To move one or more files: MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination To rename a directory: MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2 [drive:][path]filename1 Specifies the location and name of the file or files you want to move. destination Specifies the new location of the file. Destination can consist of a drive letter and colon, a directory name, or a combination. If you are moving only one file, you can also include a filename if you want to rename the file when you move it. [drive:][path]dirname1 Specifies the directory you want to rename. dirname2 Specifies the new name of the directory. /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. The switch /Y may be present in the COPYCMD environment variable. This may be overridden with /-Y on the command line. Default is to prompt on overwrites unless MOVE command is being executed from within a batch script. Back to table


This command is not supported by the help utility. Try "now /?". Back to table


PATH Displays or sets a search path for executable files. PATH [[drive:]path[;...][;%PATH%] PATH ; Type PATH ; to clear all search-path settings and direct cmd.exe to search only in the current directory. Type PATH without parameters to display the current path. Including %PATH% in the new path setting causes the old path to be appended to the new setting. Back to table


PAUSE Suspends processing of a batch program and displays the message Press any key to continue . . . Back to table


PROMPT Changes the cmd.exe command prompt. PROMPT [text] text Specifies a new command prompt. Prompt can be made up of normal characters and the following special codes: $A & (Ampersand) $B | (pipe) $C ( (Left parenthesis) $D Current date $E Escape code (ASCII code 27) $F ) (Right parenthesis) $G > (greater-than sign) $H Backspace (erases previous character) $L < (less-than sign) $N Current drive $P Current drive and path $Q = (equal sign) $S (space) $T Current time $V Windows version number $_ Carriage return and linefeed $$ $ (dollar sign) If Command Extensions are enabled the PROMPT command supports the following additional formatting characters: $+ zero or more plus sign (+) characters depending upon the depth of the PUSHD directory stack, one character for each level pushed. $M Displays the remote name associated with the current drive letter or the empty string if current drive is not a network drive. Back to table


POPD Changes to the directory stored by the PUSHD command. POPD If Command Extensions are enabled the POPD command will delete any temporary drive letter created by PUSHD when you POPD that drive off the pushed directory stack. Back to table


PUSHD Stores the current directory for use by the POPD command, then changes to the specified directory. PUSHD [path | ..] path Specifies the directory to make the current directory. If Command Extensions are enabled the PUSHD command accepts network paths in addition to the normal drive letter and path. If a network path is specified, PUSHD will create a temporary drive letter that points to that specified network resource and then change the current drive and directory, using the newly defined drive letter. Temporary drive letters are allocated from Z: on down, using the first unused drive letter found. Back to table


RMDIR Removes (deletes) a directory. RMDIR [/S] [/Q] [drive:]path RD [/S] [/Q] [drive:]path /S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree. /Q Quiet mode, do not ask if ok to remove a directory tree with /S Back to table


REM Records comments (remarks) in a batch file or CONFIG.SYS. REM [comment] Back to table


RENAME Renames a file or files. RENAME [drive:][path]filename1 filename2. REN [drive:][path]filename1 filename2. Note that you cannot specify a new drive or path for your destination file. Back to table


SET Displays, sets, or removes cmd.exe environment variables. SET [variable=[string]] variable Specifies the environment-variable name. string Specifies a series of characters to assign to the variable. Type SET without parameters to display the current environment variables. If Command Extensions are enabled SET changes as follows: SET command invoked with just a variable name, no equal sign or value will display the value of all variables whose prefix matches the name given to the SET command. For example: SET P would display all variables that begin with the letter 'P' SET command will set the ERRORLEVEL to 1 if the variable name is not found in the current environment. SET command will not allow an equal sign to be part of the name of a variable. Two new switches have been added to the SET command: SET /A expression SET /P variable=[promptString] The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence: () - grouping ! ~ - - unary operators * / % - arithmetic operators + - - arithmetic operators << >> - logical shift & - bitwise and ^ - bitwise exclusive or | - bitwise or = *= /= %= += -= - assignment &= ^= |= <<= >>= , - expression separator If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes. Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values. If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression. The assignment operator requires an environment variable name to the left of the assignment operator. Numeric values are decimal numbers, unless prefixed by 0x for hexadecimal numbers, and 0 for octal numbers. So 0x12 is the same as 18 is the same as 022. Please note that the octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits. The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty. Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. %PATH:~0,-2% would extract all but the last 2 characters of the PATH variable. Finally, support for delayed environment variable expansion has been added. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /? Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed. The following example demonstrates the problem with immediate variable expansion: set VAR=before if "%VAR%" == "before" ( set VAR=after if "%VAR%" == "after" @echo If you see this, it worked ) would never display the message, since the %VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, the following example will not work as expected: set LIST= for %i in (*) do set LIST=%LIST% %i echo %LIST% in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found. Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty. So the actual FOR loop we are executing is: for %i in (*) do set LIST= %i which just keeps setting LIST to the last file found. Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended: set VAR=before if "%VAR%" == "before" ( set VAR=after if "!VAR!" == "after" @echo If you see this, it worked ) set LIST= for %i in (*) do set LIST=!LIST! %i echo %LIST% If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below: %CD% - expands to the current directory string. %DATE% - expands to current date using same format as DATE command. %TIME% - expands to current time using same format as TIME command. %RANDOM% - expands to a random decimal number between 0 and 32767. %ERRORLEVEL% - expands to the current ERRORLEVEL value %CMDEXTVERSION% - expands to the current Command Processor Extensions version number. %CMDCMDLINE% - expands to the original command line that invoked the Command Processor. Back to table


SHIFT Changes the position of replaceable parameters in a batch file. SHIFT [/n] If Command Extensions are enabled the SHIFT command supports the /n switch which tells the command to start shifting at the nth argument, where n may be between zero and eight. For example: SHIFT /2 would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected. Back to table


START Starts a separate window to run a specified program or command. START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL] [/AFFINITY ] [/WAIT] [/B] [command/program] [parameters] "title" Title to display in window title bar. path Starting directory B Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application I The new environment will be the original environment passed to the cmd.exe and not the current environment. MIN Start window minimized MAX Start window maximized SEPARATE Start 16-bit Windows program in separate memory space SHARED Start 16-bit Windows program in shared memory space LOW Start application in the IDLE priority class NORMAL Start application in the NORMAL priority class HIGH Start application in the HIGH priority class REALTIME Start application in the REALTIME priority class ABOVENORMAL Start application in the ABOVENORMAL priority class BELOWNORMAL Start application in the BELOWNORMAL priority class AFFINITY The new application will have the specified processor affinity mask, expressed as a hexadecimal number. WAIT Start application and wait for it to terminate command/program If it is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run. If it is not an internal cmd command or batch file then it is a program and will run as either a windowed application or a console application. parameters These are the parameters passed to the command/program If Command Extensions are enabled, external command invocation through the command line or the START command changes as follows: non-executable files may be invoked through their file association just by typing the name of the file as a command. (e.g. WORD.DOC would launch the application associated with the .DOC file extension). See the ASSOC and FTYPE commands for how to create these associations from within a command script. When executing an application that is a 32-bit GUI application, CMD.EXE does not wait for the application to terminate before returning to the command prompt. This new behavior does NOT occur if executing within a command script. When executing a command line whose first token is the string "CMD " without an extension or path qualifier, then "CMD" is replaced with the value of the COMSPEC variable. This prevents picking up CMD.EXE from the current directory. When executing a command line whose first token does NOT contain an extension, then CMD.EXE uses the value of the PATHEXT environment variable to determine which extensions to look for and in what order. The default value for the PATHEXT variable is: .COM;.EXE;.BAT;.CMD Notice the syntax is the same as the PATH variable, with semicolons separating the different elements. When searching for an executable, if there is no match on any extension, then looks to see if the name matches a directory name. If it does, the START command launches the Explorer on that path. If done from the command line, it is the equivalent to doing a CD /D to that path. Back to table


TIME Displays or sets the system time. TIME [/T | time] Type TIME with no parameters to display the current time setting and a prompt for a new one. Press ENTER to keep the same time. If Command Extensions are enabled the TIME command supports the /T switch which tells the command to just output the current time, without prompting for a new time. Back to table


TYPE Displays the contents of a text file or files. TYPE [drive:][path]filename Back to table


XCOPY 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. Back to table