What is Batch File
Introduction
In OS/2, DOS, and Microsoft Windows, a batch file is a script file. It comprises commands that the command-line interpreter needs to execute, saved in a plain text file. Any command the interpreter accepts interactively may be included in a batch file, along with constructs like IF, FOR, and GOTO labels to allow for conditional branching and looping inside the file. Even if a batch file might not run a batch of many data files, the term "batch" in batch processing refers to "non-interactive execution".
Batch files, which work similarly to Job Control Language (JCL), DCL, and other systems on mainframe and minicomputer systems, were developed to aid with repetitive tasks by allowing the user to draw up a script to automate them. When a batch file is run, the shell program (cmd.exe or COMMAND.COM) executes its commands line by line. A shell script is a more versatile but comparable file type seen in Unix-like operating systems like Linux. Both Windows and DOS utilise the .bat filename extension. OS/2 and Windows NT were also introduced cmd. Different environments may use different batch file extensions. For example, 4DOS, 4OS2, and 4NT-related shells use .btm. Between versions, there have been major changes in how batch files are handled in detail. While some information in this article is relevant to all batch files, other information is specific to particular versions.
Significance of Batch Files
Batch files are important because they may automate and expedite many different computer activities. They reduce the possibility of human error, increase productivity, and save time by carrying out several tasks in a single file. Batch files are particularly helpful for organising recurring tasks, streamlining intricate procedures, and effectively completing system maintenance jobs.
Creating Batch Files
Notepad is usually used to create batch files. Thus, launching Notepad and typing the script's commands is the easiest method. Open a notebook and type the following sentences for this exercise:
:: Deletes All files in the Current Directory With Prompts and Warnings
::(Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF
DEL .
DR
Saving Batch Files:
The next step is to save your batch file once it has been produced. The extensions for batch files are either .bat or .cmd. A few general guidelines to remember when naming batch files.
- When naming batch files, avoid using spaces, as this might occasionally cause problems when the files are called from other scripts.
- Avoid naming them after widely used batch files found in the system, like ping. Cmd.
Here are a few things to consider when you save your batch file:
- Don't forget to conclude the file name with .bat or. Cmd.
- Click "Save as type" and select "All Files."
- Enclose the whole file name in quotation marks “”.
Executing Batch Files
The steps to run a batch file are as follows:
- Step 1: Launch cmd.exe, the command prompt.
- Step 2: Go to the directory where the cmd or .bat file is located.
- Step 3: To run the batch file, type the file name as it appears in the following image and hit Enter.
Modifying Batch Files
The instructions for editing an already-existing batch file are as follows.
- Step 1: Launch Windows Explorer.
- Step 2: Navigate to the directory containing the .bat or .cmd file.
- Step 3: From the context menu, select "Edit" by right-clicking on the file. Notepad will open the file so that you may make more edits.
Basic Commands in Batch Files
A user must be familiar with the fundamental commands to construct and run a batch file.
Several well-liked fundamental commands are:
- Echo: By default, it is set on but may be turned off. It lets the user see messages. Users can see the characters they input on the screen when Echo is enabled. You cannot view input when the Echo is turned off.
- Title: This command modifies the text that appears as the command window's title at the top.
- Rem: Code explanations or comments are usually provided in Rem statements. They are not a component of the code that is running.
- Cls: The prompt screen is cleared using the cls command.
- Pause: The batch file's execution is halted by the pause command. The window remains open after executing this command, allowing users to choose whether to continue or pause between each activity. The window automatically closes when the script runs if this command is unused.
- Start " [website]: This command opens a website of the user's choosing using the default browser.
- Ipconfig: The ipconfig command shows network details, including IP addresses, subnet masks, and MAC addresses.
- Ping: The ping command measures the location and latency of outgoing data packets to test an IP address.
Data Types in Batch
- Integers: The entire set of positive and negative integers is supported by Batch.
- Strings: In contrast to other programming languages, we seldom ever use double quotes("") here; instead, we print strings using the "echo" command.
Precision values, or floating-point values, are not supported by Batch.
List of Operators
SET /A sum=1+1 :: addition operator
ECHO %sum%
SET /A mul=7*9 :: multiplication operator
ECHO %mul%
SET /A div=9/3 :: Division operator
ECHO %div%
SET /A assign=10 :: Assignment operator
ECHO %assign%
SET /A assign+=15 :: Increment then assignment operator
ECHO %assign%
SET /A mod= 10%3 :: Modulus/Remainder operator
ECHO %mod%
Features of Batch Script
- Batch script features include reading user input to be processed further
- Features control structures for improved automation and scripting, such as for, if, while, and switch.
- Supports sophisticated features, including arrays and functions.
- Regular expressions are supported.
- May contain more programming languages, such as Perl.
Common Uses of Batch Script
- Configuring servers for various uses.
- Automating cleaning tasks like removing log files or unnecessary files.
- Automating the transfer of apps between environments.
- Installing software simultaneously on many computers.