Another way of getting the job done other than utilizing tasks in Windows are batch scripts. Batch scripts are a collection of command-line operations stitched together into a single executable file(.bat). In some cases, a batch script is needed to execute programs that require arguments to be defined. You no longer need to open up the commend prompt and write out the command in its entirety every time, this helps save an abundant amount of time.
Windows has something called an Command Prompt. This program dates back to the early days before Windows came to be when MS-DOS used to be a standard in computing. Everything done on the machine was through the use of “commands” – type a set of texts with arguments and having the computer execute it for you. Once mastered, there is joy and fun to be had in doing all of your work in this type of user space without having to navigate through the Windows GUI. You may even find it quicker to get stuff done! Nowadays, Powershell has been added to Windows as an more advanced form of Command Prompt to compliment it.
In order to open Command Prompt, right-click on the Start Menu and go to “Run”.
Run allows you to run any Windows command by defining a path to its executable file. For system applications, you can simply just type in the name of the program. We are going to type in “cmd” and click “OK”.
Command Prompt is now open. Here the default path to your user directory is displayed as well as an “>” sign after. You will be typing your commands here for execution. Lets go ahead and print a simple set of text. We are now going to do the simple and standard “Hello World” command. For this we will be typing the following:
echo Hello World!
The echo command prints out whatever you type right after it. You will be using this particular command quite a bit when writing batch scripts. Hit the “Enter” key on your keyboard to execute.
“Hello World!” was printed right below our original command. At some point if your Command Prompt has too much text accumulated, you can simply type in “cls” in order to clean it up.
You can obtain a list of commands to use by typing “help” into the Command Prompt. Don’t forget you can scroll through the list due to it being too long. To get an idea on how to use a specific command, you can also type something like “echo /?” and it will give you instructions on how to fill it out properly.
Creating a batch script file
In order for us to use a batch file in order to execute commands with a click of a button, we will first need to create our batch file. Right-click on the Start Menu and click “Run”. This time, we will be opening Notepad. Type “Notepad” into the box and click “OK” to open it. Afterwards, go to File->Save As.
Under the option “Save as type:” click on the drop-down menu and select “All Files (.). This allows us to define our extension as we want through our “File name:”. Name it “firstbatchscript.bat”, the “.bat” at the end designates our file as a batch script that can be executed just like any other executable file within Windows. After, click “Save”.
If you look in your folder, Windows now sees it as a “Windows Batch File”. The batch script can now execute with just a click, as well as via external programs such as Task Scheduler. Now, we are going to fill it up with some commands. Lets do what we did earlier with “Hello World!”, type the following into Notepad:
echo off echo Hello World! pause
Here, we typed in “echo off”. This keeps Command Prompt from displaying the command during execution and only the output produced. Of course next it is going to print out the phrase “Hello World!”. We have also typed out the command “pause” which prevents Command Prompt from closing right after it has been completed. It will ask you to press any key to continue.
Click on File->Save, go to the folder where it was originally place and run it.
It looks like our batch script went off without a hitch. Though our default file path and the command “echo off” seems to be displaying still. There’s a little workaround for this. Head back to Notepad and make the following changes to your script:
echo off cls echo Hello World! pause
We want to clean up the “echo off” command in our script to allow for a more professional-looking operation. Run the batch script again.
We no longer see the first command being executed as well as our file path. Lets spice it up a little, we’re now going to make it open up Notepad after the first pause:
echo off cls echo Hello World! pause notepad.exe pause
Again, the only reason we don’t need to define a file path for “notepad.exe” is because it’s a system application. You can pause for as many times as you want in the batch script, but do remember that this requires user interaction and will not function on its own. Run it again and see that you will be asked to press any key. After, Notepad should open right up and ask you to press any key again to close.
In the end, batch files make a great way for executing a bunch of commands. You are also able to automate these files through Task Scheduler, just by appending the file to the program name instead of an “.exe” file. There are tons of programs out there that rely on the use of additional commands, and makes all the more reason to utilize batch scripts.