Introduction to Batch File:
A batch file is a computer file containing Instructions that the computer should carry out once the batch file is launched.
The Term batch is from batch processing, meaning Non-interactive execution. Batch file is a kind of script file with .bat, .cmd, .btm file extensions in Operating systems which consists of a series of commands to be executed by the command line interpreter (cmd). When we run a batch file, the commands written in it are executed in the command prompt following a serial fashion.
We all know about window command line interpreter known as command prompt or cmd. CMD takes various commands as input through keyborad and processes them. So, The batch file work like a mediator between we and the command prompt. It save our time to repeat the same commands again and again.
Enough Introduction ! now let's start to learn the basic of batch file and learn how we can write our batch files in window OS. follow the given steps and enjoy your learning with InfoBrother.
creating batch file:
Open a text editor such as Notepad. We can use any text editor, But the Most used application is Notepad. Notepad commanly used to convert text files into batch files. use following steps to open Notepad.
①Press window button and search for Notepad.
②Or, Right click at anywhere and select New › Text Document.
Basic batch commands:
Batch files run a series of cmd commands or DOS commands, so the batch commands are similar to DOS commands. Some of the more important commands are:
○ ECHO: Displays text on the screen and also used to decide whether or not to display the command itself.
○ @ECHO OFF: Echo turn off - This command will make sure that the command themselves are not printed.
○ START: Run a file with its default application.
○ REM: Used to insert the comments in the program. Anything written after REM will not be executed in the program.
○ ASSOC: Used to associates an extension with a file type and displays existing associations, or delete an association.
○ CD: Show Current directory or move to different directory.
○ CLS: Clear screen.
○ COPY: Copy files from one location to other.
○ EXIT: Exit the DOC console.
○ PAUSE: Pause the process and wait for input to be entered.
○ TITLE: Edit the title of the window.
○ TYPE: Used to display the content of the file to an output console.
○ MKDIR/RMDIR:Create or Remove directories.
○ DEL: Delete a file or files.
○ HELP: Show the list of all Commands.
Writing the batch file.
To Understand, how to create the batch file, let's write a simple batch command that can read the content of a file and display it to an output console. Open your Notepad and write the given script as it is.
@echo OFF TYPE D:\textfile.txt pause
Above script read the content of textfile.txt and display it to an output console.
Saving the batch file.
Once we have done with writing script in our batch file, we can proceed with saving it as an executable file. Follow the given steps to save the file.
① Click File in the top-left corner of the Notepad window and click save - or simply press Ctrl + S to save the file.
② Write file name using .bat extension.
③ Click Save button to save the batch file.
Launch the batch file.
Once we have done with creating and saving the batch file using .bat extension, we can launch the batch file to execute the commands. To lunch the commands, Double click on the batch file short cut and the command prompt window will open quickly, executes all the commands written in batch file, and read the content from text file and display it on cmd prompt window.
Batch file save our time, otherwise, we would invested in typing the same commands again and again. For instance, We can create the batch file that can open multiple programs in just one click. Every day when i turn on my pc, i need to open Browser, a text editor, Adobe photoshop, ms word, File explorer and much more... But now i can open these all applications in just one click using batch file.
Using batch file, we can schedule our window OS to shut donw after a specific amout of time.