What are batch files and what do they do?
Batch files are not often spoken about. You may have heard it in passing, but chances are, no one has really sat down and told you what they’re used for or how they work. They’re actually a very important part of Microsoft Windows and are very handy, depending on what you use them for. Follow along below and we’ll show you what a batch file is, what it does, and how you create one.
What are batch files?
Batch files are plain text files with a bunch of commands in them for the command line interpreter to execute. You may also hear a batch file referred to as a script file. It’s important to remember that batch files are strictly Windows-based. Linux and other operating systems has their own version of the batch file for executing script commands, but “batch” is specifically for Windows.
Since batch files are plain text files filled with commands for the command line interpreter to run, it’s important to not open a batch file that you’re unfamiliar with. Opening an unknown batch file could potentially be disastrous if someone with malicious intent sent that along to you or someone else.
What do they do?
Like we’ve already mentioned, batch files are plain text files that contain commands or instructions for the command line interpreter to execute. In this case, since a batch file can only be operated within the Windows operating system, that interpreter or “shell program” would be cmd.exe, which you might recognize more as Command Prompt.
The shell program takes that batch files and executes whatever instructions are inside. These instructions inside batch files can be a bunch of different commands, but many people primarily use batch files to automate repetitive tasks.
On the malicious side of things, people can use batch files/scripts to harm your computer. Using a batch file, you can easily plant malware on your computer, delete imperative files, crash the PC and so on. So while batch files are used for a lot of good — such as automating repetitive tasks — they can be used to harm as well. That said, it’s good practice to not just run any batch file you come across. You can (usually) see the contents of a batch file by right-clicking on it and selecting Open with Notepad or your preferred plain-text editor.
How do you create one?
Creating a batch file is easy. After all, it is only just a plain text file. To create one, you can simply right-click on your desktop, select New File and call it something like test.bat. You can call it anything you’d like, as long as it has that .bat file extension at the end of it. In some cases, you may actually need to open Notepad or your plain-text editor first, and then click “Save As” and make sure that it’s being saved as a .bat file extension.
To open the batch file to add or edit the contents of it, just as we said above, you can right-click it, and open it with Notepad or any other plain-text editor. This is actually where you add commands. For example, you could put something like this in the file:
Those are simple commands, which simply outputs “Hello World” to the command line. It then waits for the user to press a key and then terminates the program.
That’s just how a very basic batch file program looks, though. They obviously get a whole lot more complicated to automate tasks, adding different commands, variables and other syntax to the mix.
Closing
That’s just a quick look at what batch files are and what they can do. Remember, batch files can only be operated within a Windows operating system environment, so you won’t be able to execute the same file extension over on Linux.
If you have any questions, be sure to leave a comment below or join us over in the PCMech Forums!