How To: Make and Work with a Windows Batch File
While this is not strictly a cryptocurrency related task, the question does come up enough that I thought I would create a few quick How To’s covering basic Windows skills. You may have seen the first one I did: How To: View Full Windows File Extensions and this is in a similar vein related to another topic that comes up quite often; creating and working with Windows batch files.
For the old school wizards, you will not find much new in these posts as they are mainly geared toward our newer users. Also this is guide is aimed more toward the site’s audience in way of explanation and examples, so I will not be going too deep into the subject.
The first page is a basic introduction to someone with little to no experience in what a batch file is or does. If you already have some basic familiarity with batch files, you may want to jump to page two.
What is a batch file. It is basically a way to communicate with the OS when you want to perform a repetitive or automated task. It is in a way a simple way to run a mini-program using the build-in command line interpreter. For a much more extensive look at batch files and their power, please consult the wonderful TechNet article that does a much better job than I will attempt.
This guide is geared for more simplistic uses and mainly to supply parameters for simple launch files to use when mining. I will go over the basics of creating simple launch files, as well as a simple way to launch your mining program upon computer start-up. This is quite useful if your computers are set to power on after power loss within the BIOS, and to quickly return to mining.
Creating a Simple Batch File
First a prerequisite: make sure you have already enabled viewing of full file extensions in Windows.
Ok, it is time to get our hands dirty.
If you prefer, you can create a temporary empty folder for now if it makes it easier. I have made a new folder named “temp” off my C: drive to make the examples easier. I can then easily delete the folder afterwards when I no longer have a use for it. Later on you will most likely be making the batch files within your mining program directories.
Creating a batch file is actually very simple. First open up a folder within Windows Explorer and right click on an empty space. A menu will appear similar to the one shown on the left, choose New -> Text Document.
This will create a new empty text document with a placeholder name as shown below. The new text document will be titled simply “New Text Document.txt”.
We will rename this file to test1.bat for now. Go ahead and rename it while the dialog is still highlighted. If you accidentally clicked off of it, simply right click on the file name and select rename. Make sure you remove the .txt extension or otherwise it will simply be a non-executable text file. When you click on ok, you will probably get the message shown below warning you about changing extensions. Click Yes as that is exactly what we want to do.
Now we should have a test1.bat file in our folder. If you double-click it now when it is empty, you will most likely get a warning message like the one shown below.
This is the first sign we created something other than a standard text file in that Windows is looking for code o run or execute. Since the file is empty Windows complains with the error message.
In order to edit batch files, we will need to right click on them and select Edit.
This will open a basically blank text window that we can edit with notepad.
In typical programming tradition, type in “echo Hello Crypto World! – This is my first batch file.”, hit return and on a new line type “pause”. Click on File – Save
Now if you double click on the file, instead of complaining, Windows will run the batch file and you should see a command window appear similar to the one shown below.
Congratulations! You have created your first batch file. Granted it is pretty simple and even a bit messy, but let’s look at what we did.
First using the echo command, we are telling the command interpreter to echo or display the text that follows. So echo is the actual command and the rest of the message is just text we want to display. The reason the formatting is not pretty and we see the actual command first, is that this is a command language and seeing the commands is useful in troubleshooting the program. I will show you in a bit how we can clean it up if we wanted to, but first let’s go over the other command we used in this batch file.
The second command used was pause. It is a simple command but very useful. Without the pause command, the window would have flashed its message and quickly closed. So quickly in fact that besides a brief flash, your probably wouldn’t even be able to read it. Pause simply tells the command interpreter to await further instructions. In this case we can see the “Press any key to continue” message indicating the command interpreter is indeed paused and waiting on us to give it a new command, in this case a key press, after which the program will close since there are no more commands.
While simple in its use, the pause command is actually quite useful when creating batch commands. By putting a pause statement at the end, we can keep the window open in case the program fails and be able to see any error messages that were generated. Pause can also be used when waiting on further actions, but in this case it is used to pause the program before closing.
Now before continuing on, let’s just for a bit of fun try and clean up our Hello World program a little visually. Modify your program with the following lines, save, and then run it. It should look much cleaner than before.
Here we used a more advanced usage of the echo command, as well as a new command cls.
Echo off turns off the command echoing we seen in the previous example. While it is usually a good idea to leave this on, I just wanted to show it is possible to format the output nicely if you so needed.
The cls (clear screen) command clears all information that appears in the Command Prompt window and returns to a blank window.
The echo. command simply tells the command interpreter to echo a blank line. In this example we have a blank line before and after our message we wanted to display.
The pause command functions as before and we are prompted for further action (press a key) before closing.
We will look at some more advanced examples on the next page.