- WikiHow is a 'wiki,' similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 12 people, some anonymous, worked to edit and improve it over time.
- EXIST command is used to check if a file exists or not. Read this article to know details of EXIST and all the other batch file commands. @echo OFF::EXIST command is used to check for existence IF EXIST D: abc.txt ECHO abc.txt found IF EXIST D: xyz.txt (ECHO xyz.txt found) ELSE (ECHO xyz.txt not found) PAUSE.
Batch files have been around since the early Windows operating system. These are plain text files with .bat|.cmd|.btm file extensions, and when executed, the commands are interpreted by the Windows command-line interface.
While Microsoft released a more advanced command-line tool called PowerShell (this is not installed by default), the native windows command line remains a popular choice for scripting. Here are some tips and tricks for developers and non-technical people alike:
Parameter Description; not: Specifies that the command should be carried out only if the condition is false. Errorlevel: Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number. I'm a software developer loving life in Charlotte, NC, an (ISC) 2 CSSLP and an avid fan of Crossfit. And, no, I'm not Steve Jansen the British jazz drummer, though that does sound like a sweet career.
- Displaying of Comments or Remarks (ECHO and REM)
Documentation is important, especially for very long batch files. However, during execution, these could clutter the display and can be difficult to read.
- Using ‘@REM' and ‘@ECHO OFF There are two differences between ECHO and REM. REM can totally be hidden during execution, while ECHO can at some level be suppressed, but will always be displayed during execution. Here are some tricks on how to change displays of comments and remarks by prepending with ‘@' character:
- @REM will hide the remark entirely and will not be shown on the command-line. All REM lines after an @ECHO OFF call will also be hidden. These are suitable for documentation intended for developers writing the script.
- ECHO by default will be displayed during command invocation, as well as on the standard output. Using @ECHO or if preceded by @ECHO OFF will display the command on the standard output only. This is suitable for creating markers during execution to show at which point the command has executed so far.
- Consider the following batch file:
Its corresponding output is:
- Using ‘@REM' and ‘@ECHO OFF There are two differences between ECHO and REM. REM can totally be hidden during execution, while ECHO can at some level be suppressed, but will always be displayed during execution. Here are some tricks on how to change displays of comments and remarks by prepending with ‘@' character:
- Error Handling
In its simplest form, batch files are straightforward in its sequential command execution. However, there are tasks that need error handling, either for notification or cleanup. Batch files, after executing a command, provides an error code called ERRORLEVEL. A non-zero value means an error has occured.
Electronic orrerydaves astro tools. Astro Pneumatic Tool Company offers a wide range of innovative automotive aftermarket tools. With over 30 years of experience in manufacturing and importing, Astro is your one-stop shop for tools and shop equipment. Astro has products, not only for military and aerospace programs, but also for other high tech fields such as computers, fiber optics and the medical industry. Astro is ready for the future. When your job requires crimping tools, insertion and removal tools or connector service kits, please remember that Astro has the tools of your trade. Astro Tool Corp 21615 SW Tualatin Valley Highway, Beaverton, OR 97003 USA Tel: 503-642-9853 Fax: 503-591-7766 Email: sales@astrotool.com Corporate Information. Dave's Astro Tools: Home Download Contact Electronic Orrery: Orreries used to be popular many years ago. They were mechanical models of the Solar System and were originally driven by turning a crank handle or by using a clockwork motor. Of course with the advent of modern computers and sophisticaed planetarium programs those old orreries have.
- ERRORLEVEL vs %ERRORLEVEL%
- Both pertain to the current error code, and has the same value. The main difference between the two is that %ERRORLEVEL% is handled just like a variable, while ERRORLEVEL is handled specifically by the if operation. So if you would like to check for errors:
The abovementioned snippets would have the same behavior. ERRORLEVEL checks if the value is equal to or greater than the number specified.
- Example error handling in Windows batch files:
The output of this would be:
Windows Batch File Error Handling Example
- ERRORLEVEL vs %ERRORLEVEL%
- Use VERIFY as a simple check for copying or moving files.
Do you copy files within your batch file? It is worth it to add a one liner command VERIFY ON, to perform a simple check of the destination file. The operation is not comprehensive, as it does not check if the file is corrupted. However, if you are transferring a lot of files (e.g. backups, migration), then this is a first step to ensure integrity of the operation.
- You can use xcopy or copy as copy operations:
Do you have other tips and tricks for creating Windows Batch Files? Comment on this page and will add it up to the list.
PushMon supports the use of Windows batch files. If you have batch files that are scheduled to execute at specific intervals, you can use PushMon to monitor its execution and notify you if something goes wrong. We have created a separate blog post on how to monitor your batch files using Windows task scheduler.
Introduction
Batch files are used to automate groups of commands which would ordinarily be executed from the command line.
Benefits of using batch files include:
- Savings in time and effort
- Simplification of operations for inexperienced users
- Simplification of complex commands for experienced users
- Elimination of operator error
Common uses for batch files include:
- Copying or deleting files
- Creating the proper environment for an application
- Setting environmental variables
A common practice is to place all batch files in one directory and then PATH to that directory. This centralization of batch files makes modifications easier if they become necessary.
Batch files are plain text files created using a plain text editor such as Notepad or the DOS EDIT text editor.
Each DOS command, and/or other external command, is placed on its own line along with all the required parameters and switches.
Batch files are executed by the command interpreter, Command.com. Command.com opens a batch file, reads the command on the first line, closes the batch file, executes the first command, and then repeats those three steps for each line in the batch file.
This rather odd method of execution can be demonstrated with the following batch file:
Commands Specific to Batch Files
Echo
Echo is a feature of batch files which repeats (echos) each line of the batch file to the screen as it is executed.
Although useful for debugging purposes, once production is completed ECHO serves no purpose other than creating a messy screen which may confuse unsophisticated users.
To 'clean up' the batch file operation, turn off ECHO by placing the ECHO OFF command on the first line of the batch file. Since the command does not take effect until after it is executed, DOS will echo that first line. To suppress the echo of that first line, place '@' in front of it. The '@' command can also be used to selectively repress echo as required.
The ECHO command also can be used to display text within a batch file to the screen. Text on a line in a batch file which is preceded by ECHO will appear on the screen regardless of whether or not the ECHO OFF command has been issued.
Rem
The command processor will ignore any line in a batch file which is preceded by the Rem command. The Rem command (Rem-ark) is frequently used at the beginning of lines which are intended to document a batch file's operation. It can also be used to disable a selected line or lines for debugging purposes.
Call
When a program (.com or .exe) is run from within a batch file, control returns to the batch file when the program is terminated. But when a command in a batch file is the name of another batch file, control does not return to the first batch. Rather, when the second batch file has terminated it will return to the command line.
Here is an illustrated of this. As written, the last line of the first of the first batch file below will never execute:
To make control return to the first batch file, insert the CALL command in front of the command to execute Testtwo.bat.After Testtwo.bat terminates, control returns to Testone.bat and execution picks up where it left off. This time the last line WILL be executed.
Batch File Examples
The following batch file examples can be pasted into a text editor andmodified to match your environment. If you paste them into a word processor, be sure that you Save As a plain text file.
Displaying a text file
When large amounts of text are to be displayed to the screen from within a batch file, a more convenient alternative to ECHOing text to the screen is to place the text in a separate file and then display the contents of that file:Or, when there is more text that can fit on one screen:
The second example takes advantage of redirection, see below.
If Noti Batch Files Rar
Deleting Temporary Files
Place the following lines in an AUTOEXEC.BAT to clean out temporary files during the boot. (Note the use of redirection, covered in detail below).
Control a Printer
Use a one-line batch file like this one to issue a form feed to a HP laser printer:
If Not Batch File
Find a Disk File
Windows cleanup utility free download. A batch file like the following one can be used to find a disk file from the command line.
The /v switch for CHKDSK is for 'Verbose', which willlist all files on the specified disk. That output is piped (|) through the FIND filter.
PushMon supports the use of Windows batch files. If you have batch files that are scheduled to execute at specific intervals, you can use PushMon to monitor its execution and notify you if something goes wrong. We have created a separate blog post on how to monitor your batch files using Windows task scheduler.
Introduction
Batch files are used to automate groups of commands which would ordinarily be executed from the command line.
Benefits of using batch files include:
- Savings in time and effort
- Simplification of operations for inexperienced users
- Simplification of complex commands for experienced users
- Elimination of operator error
Common uses for batch files include:
- Copying or deleting files
- Creating the proper environment for an application
- Setting environmental variables
A common practice is to place all batch files in one directory and then PATH to that directory. This centralization of batch files makes modifications easier if they become necessary.
Batch files are plain text files created using a plain text editor such as Notepad or the DOS EDIT text editor.
Each DOS command, and/or other external command, is placed on its own line along with all the required parameters and switches.
Batch files are executed by the command interpreter, Command.com. Command.com opens a batch file, reads the command on the first line, closes the batch file, executes the first command, and then repeats those three steps for each line in the batch file.
This rather odd method of execution can be demonstrated with the following batch file:
Commands Specific to Batch Files
Echo
Echo is a feature of batch files which repeats (echos) each line of the batch file to the screen as it is executed.
Although useful for debugging purposes, once production is completed ECHO serves no purpose other than creating a messy screen which may confuse unsophisticated users.
To 'clean up' the batch file operation, turn off ECHO by placing the ECHO OFF command on the first line of the batch file. Since the command does not take effect until after it is executed, DOS will echo that first line. To suppress the echo of that first line, place '@' in front of it. The '@' command can also be used to selectively repress echo as required.
The ECHO command also can be used to display text within a batch file to the screen. Text on a line in a batch file which is preceded by ECHO will appear on the screen regardless of whether or not the ECHO OFF command has been issued.
Rem
The command processor will ignore any line in a batch file which is preceded by the Rem command. The Rem command (Rem-ark) is frequently used at the beginning of lines which are intended to document a batch file's operation. It can also be used to disable a selected line or lines for debugging purposes.
Call
When a program (.com or .exe) is run from within a batch file, control returns to the batch file when the program is terminated. But when a command in a batch file is the name of another batch file, control does not return to the first batch. Rather, when the second batch file has terminated it will return to the command line.
Here is an illustrated of this. As written, the last line of the first of the first batch file below will never execute:
To make control return to the first batch file, insert the CALL command in front of the command to execute Testtwo.bat.After Testtwo.bat terminates, control returns to Testone.bat and execution picks up where it left off. This time the last line WILL be executed.
Batch File Examples
The following batch file examples can be pasted into a text editor andmodified to match your environment. If you paste them into a word processor, be sure that you Save As a plain text file.
Displaying a text file
When large amounts of text are to be displayed to the screen from within a batch file, a more convenient alternative to ECHOing text to the screen is to place the text in a separate file and then display the contents of that file:Or, when there is more text that can fit on one screen:
The second example takes advantage of redirection, see below.
If Noti Batch Files Rar
Deleting Temporary Files
Place the following lines in an AUTOEXEC.BAT to clean out temporary files during the boot. (Note the use of redirection, covered in detail below).
Control a Printer
Use a one-line batch file like this one to issue a form feed to a HP laser printer:
If Not Batch File
Find a Disk File
Windows cleanup utility free download. A batch file like the following one can be used to find a disk file from the command line.
The /v switch for CHKDSK is for 'Verbose', which willlist all files on the specified disk. That output is piped (|) through the FIND filter.
The replaceable parameters %1 and %2 stand in for the drivedesignation and the filename being searched for and make the batch file moreflexible. (See replaceable parameters below for more information about them).
Advanced Batch File Techniques
Redirection
All DOS commands have some output. For example, the output of the DIRcommand is a directory listing, while the output of the COPY command is '1 file(s) Copied'.
The default destination for all command output is the device named CON -the console, or monitor. Command output can, however, be redirected to otherDOS devices such as PRN, NUL, or a disk file. The signs of redirection are > and <.
Redirection works because DOS treats all devices as file handles. Study the examples in the table below:Redirected Command | Result |
DIR > PRN | Printout of the directory listing on the default printer |
DIR > dir.txt | A directory listing written to the file DIR.TXT (If the file DIR.TXT already exists, it is overwritten) |
DIR >> dir.txt | A directory listing written to the file DIR.TXT (If the file DIR.TXT already exists, the directory listing is appended to the end of the file) |
CHKDSK >> sysinfo.txt | The output of CHKDSK is written to the file SYS.INFO.TXT If the file already exists the output is appended to the end of the file |
COPY filename > NUL | The output of COPY (1 file(s) Copied) is sent to the NUL device. The NUL device, otherwise known as the 'bit bucket', is a test device which is a virtual dead-end. In other words, the output gets sent nowhere. This technique is commonly used to hide batch file screen output which cannot otherwise be suppressed with ECHO OFF. |
Redirection can also be used to respond to DOS commands. For example,if we put a command to delete all files at some location (DEL [path]*.*) in a batch file,the delete command will pause the batch file execution to ask 'Are You Sure?'.
To avoid this we can redirect the contents of a file containing 'Y' (yes)and a carriage return (Enter) as input to the DEL command:
First, in the batch file folder, create a text file named YES containing the letter 'y' followed by a carriage return. Then, in a batch file use a line like:
Replaceable parameters
Replaceable parameters pass additional input provided by the user fromthe command line to a specified place within a batch file. This makes it possible to write more flexible batch files.
Create a batch file such as this:
Execute the batch file this way:
And the batch file will switch to the dbase folder and execute dbase.exe.
Execute the batch file this way:
And the batch file will switch to the myeditor folder and execute ed.exe.
IF and GOTO
The combination of the IF statement and the GOTO statement can be used to create branching logic within a batch file.
For example, if a batch file is designed to function with a replaceable parameter itis desirable to make a test to see whether or not the user actually enteredthe parameter at the command line and bail out if none was given.
Study the following example:
In this example, IF testing and GOTO <:LABEL> is used to prevent the accidental formatting of C:
LOOP
The LOOP command permits the creation of batch files that repeat:
Concatenation
Command | Result |
COPY FILE1 + FILE2 FILE3 | Combines files 1 and 2 in file 3 |
COPY FILE1 + FILE2 | Appends file 2 to end of file 1 |
COPY FILE + | Update date and time of file |
COPY FILE + /b | As above for a .COM file (/b ignores Ctrl Z) |