Beatrice is a Danish university student soon to begin her third semester of studying software.
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 such 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 are 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
- Batch scripts are ideal for processing a large number of files (renaming, deleting, moving, etc.)
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 SS64 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. Feel 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 programming languages such as C#, you don't need Visual Studio or any kind of compiler. 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, as it's great for various scripting languages. It's super convenient as it uses different colors for different types of commands, making your script easier to navigate (also known as highlighting).
So, if you feel like it, go download Notepad++ as it'll make things much easier! You can get it from Notepad++'s website.
Other editors exist as well, of course. Another recommendation of mine would be Github's Atom. Atom is also useful when dealing with more high-level languages.
Recommended for you
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 this will make them 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!)
Creating a Batch Script
First of all, we have to let batch know 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. After this, we can start writing our script.
Important Note: Leaving echo on can be very useful for debugging, but as a newbie, I recommend turning it off to avoid confusion.
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 programming and scripting are what I do.
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!
This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.
© 2018 Beatrice Sloan