INFORMATION ON BATCH FILES
BATCH FILE ABCs
Batch files are programs that allow MS-DOS and Windows users to create a file to perform a long tasks fast. Such as opening certain programs ran frequently, deleting or managing files, etc. Simple batch files do not require any special programming skills and usually can be done by knowing the below commands and or just DOS commands.
A prime example of a batch file for someone whom may know more about Windows or MacOS would be relating a batch file to a shortcut in Windows or icon on a MacOS. Batch files like a shortcuts are small file which is used to run frequently accessed programs.
Another example of a very well known batch file is the autoexec.bat, which is simply a boot file loaded each time the computer is loaded. Within this file contains various lines to load various programs and drivers into memory each time the computer boots.
CREATING A BATCH FILE
To create a basic batch file first open a MS-DOS command prompt and follow the below instructions:
At the MS-DOS prompt type edit test.bat and press enter.
If typed properly you should now be in a blue screen within the blue screen type:
pause
dir c:\windows
dir c:\windows\system
Once the above three lines have been typed in the blue screen choose file and choose exit, when prompted to save click yes
Once you are back at the MS-DOS prompt type test. When enter is pressed, this should execute the test.bat file and begin running the file. Because the first line is pause you will first be prompted to press a key. Once you press a key the batch file will run line by line in this case listing the files in the windows and windows\system directory.
If you wish to add more lines to this batch file you would simply type "edit test.bat" to edit the file again.
BATCH COMMANDS
Just like all MS-DOS commands when creating batch files the lines are not case sensitive. However, in the below listing we have listed all commands in all caps to help you identify which text is a batch file command.
@ Does not echo back the text after the at symbol. This is most commonly used as @ECHO OFF to prevent any of the commands in the batch file from being displayed just the information needed.
%1 The percent followed by a numeric value, beginning with one, allows users to add variables within a batch file. The following is a batch file example:
echo Hello %1
When the above one line batch file is created and adding your name after the batch file. For example typing myname (being the name of the bat file) and then your name:
myname bob
would output
Hello bob
:LABEL By adding a colon in front of a word such as LABEL you create a category, more commonly known as a label. This allows you to skip to certain sections of a batch file such as the end of the batch file. Also see GOTO.
CALL This is used to run another batch file within a batch file. When the batch file that is called is completed the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.
CHOICE See running different programs for example of how to use this command.
CLS Just like the DOS command would clear your screen.
ECHO Will echo a message in the batch file. Such as "ECHO Hello World" will print Hello World on the screen when executed. Note if you have not typed @ECHO OFF at the beginning of the file this will also print "ECHO Hello World" and "Hello World". If you would just like to create a blank line type ECHO. adding the period creates an empty line.
EXIT Exits out of the DOS window if the batch file is ran from Windows.
GOTO LABEL Used to go to a certain label such as LABEL. An example of GOTO would be to
GOTO END. For an Example of this see running different programs.
IF Used to check for a certain condition if the condition exists. If that condition exists it will perform that function.. To see an example of this see running different programs.
PAUSE Prompt the user to press any key to continue.
REM Allows you to place comments into the batch file without displaying or executing that line when the batch file is ran.
SHIFT Changes the position of replaceable parameters in a batch program. You can use this command only in batch programs.
START Used for Windows 95, Windows 98, and Windows NT 4.0 and above to start a windows application such as START C:\WINDOW\CALC would run the Windows Calculator. Users running Windows 3.x need to utilize the WIN command. For example WIN C:\WINDOWS\CALC.EXE would run Windows and then Calculator after Windows has finish loading.
BATCH FILE EXAMPLES
Running different programs
Deleting files in directory without prompt
How to start Windows 95 / 98 files from batch file
Creating a batch file delay
How to make time log
RUNNING DIFFERENT PROGRAMS
The following is a simple example of how you can implement the choice options into your batch files. Each line that is in red can be left out of the batch file. They have been included to help explain some of what the batch file means.
@ECHO OFF
REM - LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN
CLS
REM - THE BELOW LINE GIVES THE USER 3 CHOICES (DEFINED AFTER /C:)
CHOICE /N /C:123 PICK A NUMBER (1, 2, or 3)%1
REM - THE NEXT THREE LINES ARE DIRECTING USER DEPENDING UPON INPUT
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
HOW TO START APPLICATIONS FROM A BATCH FILE
To run files for Windows 95 or Windows 98 in a batch file you would use the START command. The following example would run Windows Notepad.
START /M "C:\WINDOWS\NOTEPAD.EXE"
The /m representing it to start the window Maximized.
CREATING A BATCH FILE DELAY
The following is an example of how to delay a batch file any where from 5 to 99 seconds. In the below example we illustrate a 5 second delay.
TYPE NUL | CHOICE.COM /N /CY /TY,5 >NUL
Additionally you could use the sleep file found on our utility download section.
HOW TO MAKE A TIME LOG
In the following example you will have the capability of creating a time log of when the batch file is loaded or for example in the autoexec.bat when someone logs into a computer.
ECHO. |TIME > TIME
COPY LOG +TIME
An alternate slightly more complicated method that to our knowledge cannot be used in Windows NT, Windows 2000 or Windows ME would be the following:
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log
For the above batch file to work properly you must create a file called log by typing "edit log" (without the quotes) and then save an exit the file which will create the file at 0 bytes. If this file is not created or not created properly you will receive the error message "Content of destination lost before copy".
How to restart the computer with a batch file:
It may be necessary after a batch file is completed its copying or installing process to restart the computer to complete that installation.
Solution:
MS-DOS
If the computer needs to be restarted from MS-DOS please see our debug page for additional information on how to do this.
Windows
If the computer needs to be restarted from Windows 98 within the batch file type:
START C:\WINDOWS\RUNDLL.EXE user.exe,exitwindowsexec
EXIT
When typing the above two lines spacing is important. It is also very important that the exit line be placed into the batch file as Windows cannot exit the window unless it is specified.
Additionally you may also type the following line in:
rundll32.exe shell32.dll,SHExitWindowsEx n
Where n is equal to one of the following numbers for the proper action.
0 - LOGOFF
1 - SHUTDOWN
2 - REBOOT
4 - FORCE
8 - POWEROFF
BATCH FILE ABCs
Batch files are programs that allow MS-DOS and Windows users to create a file to perform a long tasks fast. Such as opening certain programs ran frequently, deleting or managing files, etc. Simple batch files do not require any special programming skills and usually can be done by knowing the below commands and or just DOS commands.
A prime example of a batch file for someone whom may know more about Windows or MacOS would be relating a batch file to a shortcut in Windows or icon on a MacOS. Batch files like a shortcuts are small file which is used to run frequently accessed programs.
Another example of a very well known batch file is the autoexec.bat, which is simply a boot file loaded each time the computer is loaded. Within this file contains various lines to load various programs and drivers into memory each time the computer boots.
CREATING A BATCH FILE
To create a basic batch file first open a MS-DOS command prompt and follow the below instructions:
At the MS-DOS prompt type edit test.bat and press enter.
If typed properly you should now be in a blue screen within the blue screen type:
pause
dir c:\windows
dir c:\windows\system
Once the above three lines have been typed in the blue screen choose file and choose exit, when prompted to save click yes
Once you are back at the MS-DOS prompt type test. When enter is pressed, this should execute the test.bat file and begin running the file. Because the first line is pause you will first be prompted to press a key. Once you press a key the batch file will run line by line in this case listing the files in the windows and windows\system directory.
If you wish to add more lines to this batch file you would simply type "edit test.bat" to edit the file again.
BATCH COMMANDS
Just like all MS-DOS commands when creating batch files the lines are not case sensitive. However, in the below listing we have listed all commands in all caps to help you identify which text is a batch file command.
@ Does not echo back the text after the at symbol. This is most commonly used as @ECHO OFF to prevent any of the commands in the batch file from being displayed just the information needed.
%1 The percent followed by a numeric value, beginning with one, allows users to add variables within a batch file. The following is a batch file example:
echo Hello %1
When the above one line batch file is created and adding your name after the batch file. For example typing myname (being the name of the bat file) and then your name:
myname bob
would output
Hello bob
:LABEL By adding a colon in front of a word such as LABEL you create a category, more commonly known as a label. This allows you to skip to certain sections of a batch file such as the end of the batch file. Also see GOTO.
CALL This is used to run another batch file within a batch file. When the batch file that is called is completed the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.
CHOICE See running different programs for example of how to use this command.
CLS Just like the DOS command would clear your screen.
ECHO Will echo a message in the batch file. Such as "ECHO Hello World" will print Hello World on the screen when executed. Note if you have not typed @ECHO OFF at the beginning of the file this will also print "ECHO Hello World" and "Hello World". If you would just like to create a blank line type ECHO. adding the period creates an empty line.
EXIT Exits out of the DOS window if the batch file is ran from Windows.
GOTO LABEL Used to go to a certain label such as LABEL. An example of GOTO would be to
GOTO END. For an Example of this see running different programs.
IF Used to check for a certain condition if the condition exists. If that condition exists it will perform that function.. To see an example of this see running different programs.
PAUSE Prompt the user to press any key to continue.
REM Allows you to place comments into the batch file without displaying or executing that line when the batch file is ran.
SHIFT Changes the position of replaceable parameters in a batch program. You can use this command only in batch programs.
START Used for Windows 95, Windows 98, and Windows NT 4.0 and above to start a windows application such as START C:\WINDOW\CALC would run the Windows Calculator. Users running Windows 3.x need to utilize the WIN command. For example WIN C:\WINDOWS\CALC.EXE would run Windows and then Calculator after Windows has finish loading.
BATCH FILE EXAMPLES
Running different programs
Deleting files in directory without prompt
How to start Windows 95 / 98 files from batch file
Creating a batch file delay
How to make time log
RUNNING DIFFERENT PROGRAMS
The following is a simple example of how you can implement the choice options into your batch files. Each line that is in red can be left out of the batch file. They have been included to help explain some of what the batch file means.
@ECHO OFF
REM - LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN
CLS
REM - THE BELOW LINE GIVES THE USER 3 CHOICES (DEFINED AFTER /C:)
CHOICE /N /C:123 PICK A NUMBER (1, 2, or 3)%1
REM - THE NEXT THREE LINES ARE DIRECTING USER DEPENDING UPON INPUT
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
HOW TO START APPLICATIONS FROM A BATCH FILE
To run files for Windows 95 or Windows 98 in a batch file you would use the START command. The following example would run Windows Notepad.
START /M "C:\WINDOWS\NOTEPAD.EXE"
The /m representing it to start the window Maximized.
CREATING A BATCH FILE DELAY
The following is an example of how to delay a batch file any where from 5 to 99 seconds. In the below example we illustrate a 5 second delay.
TYPE NUL | CHOICE.COM /N /CY /TY,5 >NUL
Additionally you could use the sleep file found on our utility download section.
HOW TO MAKE A TIME LOG
In the following example you will have the capability of creating a time log of when the batch file is loaded or for example in the autoexec.bat when someone logs into a computer.
ECHO. |TIME > TIME
COPY LOG +TIME
An alternate slightly more complicated method that to our knowledge cannot be used in Windows NT, Windows 2000 or Windows ME would be the following:
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log
For the above batch file to work properly you must create a file called log by typing "edit log" (without the quotes) and then save an exit the file which will create the file at 0 bytes. If this file is not created or not created properly you will receive the error message "Content of destination lost before copy".
How to restart the computer with a batch file:
It may be necessary after a batch file is completed its copying or installing process to restart the computer to complete that installation.
Solution:
MS-DOS
If the computer needs to be restarted from MS-DOS please see our debug page for additional information on how to do this.
Windows
If the computer needs to be restarted from Windows 98 within the batch file type:
START C:\WINDOWS\RUNDLL.EXE user.exe,exitwindowsexec
EXIT
When typing the above two lines spacing is important. It is also very important that the exit line be placed into the batch file as Windows cannot exit the window unless it is specified.
Additionally you may also type the following line in:
rundll32.exe shell32.dll,SHExitWindowsEx n
Where n is equal to one of the following numbers for the proper action.
0 - LOGOFF
1 - SHUTDOWN
2 - REBOOT
4 - FORCE
8 - POWEROFF
MS-DOS batch programming is a powerful way to automate tasks on older systems. However, for modern applications running on containers, the Kubernetes Dashboard is an invaluable tool for monitoring and managing your clusters, giving you a more intuitive approach to handling system operations and performance.
ReplyDelete