What are some of the lesser know, but important and useful features of Windows batch files?
Guidelines:
- One feature per answer
- Give both a short description of the feature and an example, not just a link to documentation
- Limit answers to native funtionality, i.e., does not require additional software, like the Windows Resource Kit
Clarification: We refer here to scripts that are processed by cmd.exe, which is the default on WinNT variants.
(See also: Windows batch files: .bat vs .cmd?)
Rather than litter a script with REM or :: lines, I do the following at the top of each script:
Note how you can use the pipe and redirection characters without escaping them.
Line continuation:
Total control over output with spacing and escape characters.:
To hide all output from a command redirect to >nul 2>&1.
For example, the some command line programs display output even if you redirect to >nul. But, if you redirect the output like the line below, all the output will be suppressed.
EDIT: See Ignoring the output of a command for an explanation of how this works.
Don't have an editor handy and need to create a batch file?
Just type away the commands, press enter for a new line. Press Ctrl-Z and Enter to close the file.
Not sure how useful this would be in a batch file, but it's a very convenient command to use in the command prompt:
This will open up Windows Explorer in the "some_directory" folder.
I have found this a great time-saver.