On DOS operating systems, BAT or batch files are executable files that are processed or executed sequentially, also called batch files.
These files have the extension «.bat» (sometimes «.cmd» in Windows NT and OS/2; and «.btm» in 4DOS, 4OS2 and 4NT) and are plain text, so they can be edited in any word processor. texts.
A .BAT file contains a series of instructions that, when executed, are processed sequentially (generally one command after another in the order written, although it also has basic control structures to alter the order such as «if», «for», » goto», etc.) allowing you to automate various tasks.
Any command accepted by MS-DOS, OS/2, or the Windows command line can be entered into a .BAT, automating processes such as copying, renaming, sending data, etc.
The shell program in charge of executing these files is command.com (in DOS) or cmd.exe (in Windows).
Write and run BAT files on Windows
In Windows, you open the Command Prompt (CMD.exe) console to run a BAT file.
Currently these files are used in Windows to facilitate tedious or repetitive tasks, especially related to file and folder control.
Notepad is the ideal editor for creating a .BAT file on Windows, although it can be created in any other editor as long as the plain text is saved.
If you want commands to be highlighted for easy typing and automatically checked for errors, it is suggested to download the Notepad++ editor.
Batch file extensions
.bat
The first filename extension used by Microsoft for batch files. This extension works with DOS and all versions of Windows, under COMMAND.COM or cmd.exe, despite the different ways the two shells execute batch files.
.cmd
It is used for Windows NT family batch files and is sent to cmd.exe for interpretation. COMMAND.COM does not recognize this filename extension, so cmd.exe scripts are not run in the wrong Windows environment by mistake. Also, the append, dpath, ftype, set, path, assoc, and prompt commands, when executed from a .bat file, alter the value of the error-level variable only on error, while from within a .bat file .cmd will affect the error level even when it returns without error.
.btm
The extension used by 4DOS, 4OS2, 4NT and Take Command. These scripts are faster, especially longer ones, since the script is loaded completely ready for execution, rather than line by line.
Batch file parameters
COMMAND.COM and cmd.exe support a number of special variables (%0, %1 through %9) to refer to the batch job’s path and name and to the first nine call parameters from within the batch job. Nonexistent parameters are replaced with a zero-length string. They can be used similar to environment variables, but are not stored in the environment. Microsoft and IBM refer to these variables as replacement parameters or replaceable parameters.
Batch file example
This sample batch file displays Hello World!, prompts and waits for the user to press a key, and then exits. (Note: It doesn’t matter if the commands are lowercase or uppercase unless they work with variables)
@ECHO OFF
ECHO Hello World!
PAUSE
To run the file, it must be saved with the .bat extension (or .cmd for Windows NT-type operating systems) in plain text format, usually created using a text editor such as Microsoft Notepad or a word processor running in Windows NT mode. plain text.
When executed, the following is displayed:
Hello World!
Press any key to continue. . .
explanation
The interpreter plays each line in turn, starting with the first. The @ symbol at the beginning of any line prevents the prompt from displaying that command while it is running. The ECHO OFF command disables the indicator permanently or until it is enabled again. The @ECHO OFF combination is often like here the first line of a batch file, preventing the commands, including, from being displayed.
Then the following line is executed and the command ECHO Hello World! results in Hello World!
The following line is executed and the PAUSE command is displayed. Press any key to continue. . and stops the execution of the script.
After pressing a key, the script ends, since there are no more commands.
On Windows, if the script is run from a command line window that is already running, the window remains open on the command line as in MS-DOS; otherwise, the window closes on completion.
.BAT malware
As with any other programming language, batch files can be used maliciously. Simple trojans and fork bombs are easily created, and batch files can poison DNS by modifying the hosts file. Batch viruses are possible, and can also be spread via USB flash drives using the autorun capability of Windows.
Basic commands of .BAT files
Batch files use the same language as the command line. All you are doing is telling the command prompt what you want to enter via a file, instead of typing it into the command prompt. This saves you time and effort. It also allows you to introduce some logic (such as simple loops, conditional statements, etc., that procedural programming is conceptually capable of).
@echo: This parameter will allow you to see your working script on the command line. This parameter is useful for viewing your working code. If you run into any problems with the batch file, you can see the problems associated with your script by using the echo function. Adding a trace to this parameter will allow you to quickly close your script after it has finished.
title: Providing the same function as a <title> tag in HTML, this will provide a title for your batch script in your Command Prompt window.
cls: Clears the command line, which is best used when external code may cause what it is accessing to have to be found.
rem: The comment shorthand provides the same functionality as the tag
%%a: Each file in the folder.
(«.\»): The root folder. When using the command line, the command line must be directed to a particular directory before renaming a file, deleting a file, etc. With batch files, you just need to paste your .bat file into the directory of your choice.
pause: Allows a break in the logical chain of your .bat file. This allows users to read the command lines before proceeding with the code. The phrase «Press any key to continue…» indicates a pause.
start «» : You will be directed to a website of your choice using your default web browser. Example: start «»
ipconfig: This is a classic command line parameter that releases information regarding network information. This information includes MAC addresses, IP addresses, and subnet masks.
ping: Ping an IP address, sending data packets through the server’s paths to measure its location and latency (response time).
Example of a more advanced BAT
@ECHO OFF
:: This bat file reveals information and settings about the computer’s operating system, hardware, and network.
TITLE System Information
ECHO Please wait… checking the system.
:: Section 1: operating system (OS) information.
ECHO =============================
ECHO SO INFO
ECHO =============================
systeminfo | findstr /c:»OS Name
systeminfo | findstr /c:»OS Version»
systeminfo | findstr /c:»System type»
:: Section 2: Hardware information.
ECHO =============================
ECHO HARDWARE INFO
ECHO =============================
systeminfo | findstr /c:»Total physical memory»
wmic cpu get name
:: Section 3: Network Information.
ECHO =============================
ECHO NETWORK INFO
ECHO =============================
ipconfig | findstr IPv4
ipconfig | findstr IPv6
PAUSE
Run a .BAT file in Windows as Administrator
If you are running a command that requires administrator privileges, you must run the script as an administrator by right-clicking the batch file and selecting the Run as administrator option.
Execute the BAT file according to the task scheduler
To schedule a batch file in Windows 10, you will need to use Task Scheduler with these steps:
– Open Home.
– Find Task Scheduler and click the top result to open the experience.
– Right-click on the «Task Scheduler Library» branch and select the New Folder option.
– Type a name for the folder, for example, MyScripts.
– Click the OK button.
– Expand the «Task Scheduler Library» branch.
– Right click on the MyScripts folder.
– Select the option Create basic task.
– In the «Name» field, type a short descriptive name for the task, for example, SystemInfoBatch.
– In the «Description» field, create a description for the task. (This step is optional.)
– Click the Next button. Select the Monthly option.
Task Scheduler allows you to select from a number of triggers, including on a specific date, during startup, or when you or a particular user logs on. Depending on your needs, you will have to configure additional parameters. In this case, we are selecting the option to run a task every month.
– Click the Next button.
– Using the «Start» settings, specify when the task should start running and the time.
– Use the «Monthly» dropdown menu to choose the months of the year in which you want to run the task.
– Use the «Days» or «On» drop-down menu to specify the days the task will run.
– Click the Next button.
– Select the Start a program option to run the batch file.
– In the «Program/script» field, indicate the path of the batch file.
– Click the Finish button.
Doubts? needs more information? Write and we will respond to your email: click here