How to make a blank line in a batch file
Sometimes you would like a blank line or two in the output from a batch file. It isn't immediately clear how to do this. Simply entering "echo" doesn't work because that will output the status of command echoing. The trick is to enterecho.
Note that "echo" is followed by a period with no space in between.
Check if a file exists
A special variant of the "If" statement can be used to find out if a file is already present. The statement isif exist somefile somecommand
The statement can also test for non-existence of a file with if not exist somefile somecommand
The useful device "nul"
The invisible null device called "nul" has a number of uses. (It's also sometimes called the "bit bucket" or the "black hole".) Anything sent to it disappears. It can be used in statements when you do not want output to be dIsplayed. For example the command somecommand > nul
will carry out some command but send whatever is the normal output into oblivion. Sometimes in a batch file you do not want any possible error messages to be displayed. This is done by using somecommand 2> nul
Another use for nul is to apply a command, which normally works only on files, to a directory instead. For example, it's useful sometimes to check if a directory already exists. The statement if exist something somecommand
will check for the existence of a file but doesn't work for a directory. However all directories contain the null device so you can use this statement if exist somedirectory\nul
to check if a directory already exists.
Stopping a runaway command
Sometimes you start a command only to find that it is going on and on, spewing out screen after screen of output. Most of the time you can terminate a command by simultaneously pressing the two keys "Ctrl" and "c".Pausing a scrolling screen
If you have a command with a lot of output,, you can pause the scrolling so that you can read what's on the screen. Use the keyboard combination "Ctrl+s". To resume scrolling, repeat Ctrl+sUse drag and drop
Having to type the fully qualified path of a file every time it's needed in a command can be tedious and subject to error. Many people are unaware that a file can be dragged from a folder or Windows Explorer view and dropped on an open command window. It saves a lot of typing.Go up one level above the working directory
Any Unix user knows this one but it's often new to Windows users. To go up to the directory that is one level above the working directory, entercd ..
You can repeat this to go up more levels. It's a little off the subject of the command shell but in the Start-Run line just entering the two periods ".." will also take you up one level from the default working directory (the working directory is normally %USERPROFILE%)
Watch out for spaces in file and folder names
The command shell does not recognize spaces in path names. Any path name with spaces must be enclosed in quotation marks. This problem often crops up in scripts where certain environment variables or input arguments are used. For safety, variables that involve file or folder names should be enclosed in quotes.
Special treatment of variables in "For" statements in batch files
"For" statements are very useful, providing powerful iterative methods. They have the peculiarity, however, of requiring double percent signs for iteration variables in batch files. in other words the syntax in a batch file isfor %%variable In set Do statement
If a "For" loop is run directly from the command line, only a single percent sign is used. The syntax is then for %variable In set Do statement
Case-sensitive variables in "For" statements
In contrast to Unix systems, Windows is usually not case-sensitive, However, iteration variables in "For" statements are case-dependent. So %A is a different variable from %a.