What is CMD and Batch?
CMD is currently the default command line interpreter of Windows, slowly being replaced by Powershell, but more on that another time.
CMD is used for simple things like displaying your device's IP address or for more advanced tasks as wiping entire hard drives.
A Batch script is basically the commands of CMD put together to run in a certain order as a script. This can, like the commands, be used for simple things or more complicated purposes (An example of my own being a poorly optimized, but still working, chat program).
Why use Batch?
You might be thinking: "Why use such a limited language when C++ and Java exist?", and it's an excellent question!
I have my own personal reasons for using Batch in most cases, but here is some general advantages:
- Batch is very simple to learn
- Batch scripts can be run on every Windows system without having to install anything at all (not that this doesn't apply to other languages)
- It's fast to use if you just want to make a simple tool
Powershell is also an excellent choice, however, it's a bit more complicated to wrap your head around at times.
So, how does this work?
Well, first of all, you should smack that Windows key and then r (Win+r) to bring up Run, type in 'cmd' and hit enter.
This will present you with the classic black and white (sometimes green, depending on your system) command prompt. From there on, you can run the 'help' command as I did in the picture above and study some of the commands.
For more detailed descriptions of each command, enter the name of the command followed by '/?', for example:
If you're not comfortable with the way CMD itself explains it, you can head over to this site for even more details and examples on how to use the commands. If you're still not following, there are tons of other websites and forums explaining the commands of CMD, you're free to contact me as well if you ever need any help!
What do I need to get started?
That's what I like the most about Batch, you need absolutely nothing to get started. Unlike C++ you don't need Visual Studio or something like that, you can write Batch scripts in Notepad if that's what you want to do.
Of course, tools exist to make scripting easier for you, like Notepad++.
I highly recommend getting this program no matter what language you're writing in as it supports a lot of them, including Batch. It's super convenient as it uses different colors for different types of commands, making your script easier to navigate.
So, if you feel like it, go download Notepad++ as it'll make things much easier! You can get it here.
As I have a couple of personal preferences when writing Batch scripts, I've made this little list for you to seek inspiration in when preparing to write scripts:
- A text editor (Preferably Notepad++)
- A directory structure that's easy to navigate
- Proper naming of your files as it will make it much easier to find again
- Some good music to help you concentrate (If you prefer silence when scripting, that's totally fine as well)
- A drink, as you can spend a lot of time with this (Just five more minutes!)
Just five more minutes!
— Clive Hicks
Creating a Batch script
First of all, we have to tell Batch that we don't want it to tell us when it executes a command. We do this by putting this on the first line:
This will get rid of unnecessary output.
Leaving echo on can be very useful for debugging, but as a newbie, I recommend turning it off to avoid confusion.
After this, we can start writing our script.
I highly recommend playing around with the commands in CMD, but beware! Some of the commands can really mess up your system if you're not careful. You could end up deleting entire drives if you don't know what you're doing.
Please play respectfully, my friends. Make sure that you know exactly what the command can do before using it.
With that said, let's move on to a couple of simple commands you can use to make your script do something:
Outputs whatever you put inside the quotation marks
Changes the title of the command window to whatever you want
Changes the fore- and background colors of the command window (Please refer to the 'color /?' command for color codes)
Jumps to the label with the name specified
This is what the goto command jumps to if the name of the label is the same
Clears the screen of any output
timeout /t [time]
Waits the specified amount of seconds
Deletes the specified file
Waits for the user to press any button before continuing
Closes the application
Finishing a script
When you're finished writing your commands in a script, all you have to do is save it as a Batch file, with the extension .bat
It's really that simple! Now you should be able to double-click your new file and it should run perfectly (granted there are no errors of course).
Example of a Batch script (Base64 Encoder)
Okay, I admit that this one doesn't seem simple at all at first, but bear with me.
When you break it down it's actually pretty simple, just a bunch of echo's and goto's.
Let me quickly explain some of the other commands used here: (The following commands can do more than explained here, however, they are explained with the script above in mind)
- set /p [variable] - The set command with the /p parameter lets the user enter something and sets the variable to that.
- if [variable]==[value] - The if statement checks if a variable is equal to a given value or another variable
- type [filename] - This one just outputs the contents of a file
- certutil - This command is a bit complicated and I won't go into too much depth with it, but basically, it's able to encode and decode strings (In this case Base64)
To finish things off, I would just like to thank you for reading my basic guide on CMD and Batch scripts and I hope that I'll get to write more about this subject in the future as it's pretty much my main hobby.
If you have any questions, or if I've been unclear in any way, feel free to contact me, either through the comments at the bottom of the page or through my account. I'm always willing to help!
A little poll to end on
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.