Corrected incorrect information on step 2 (to do with limitations with parameters).
Added a couple of lines about an auto-installer in step 4.
This instructable will cover the basics of creating your own commands (and even installing them on others computers) which you can use to further cut down the amount of work you need to do.
Some things you should have:
- A Windows computer (preferably modern)
- A USB (for transporting the files around)
- A basic to advanced understanding of the syntax involved with batch commands (this is not a tutorial to help you learn about commands, this is a tutorial about making your own
- Basic computer know how (for instance, knowing how to navigate through folders)
If you don't have a basic understanding of the syntax involved with batch scripts and the like, feel free to either search for a tutorial, ask questions or comment on something you don't understand.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What to Make?
Before you create your world-famous new command, you need to figure out what to make.
Most commands have one specific purpose (such as the REN command), and each switch adds some more functionality to that command. Most of these commands were made because it would be impossible to do a certain task (that batch files are commonly used for) without them. Unfortunately, you cannot make a command that will make something possible that has never been possible before (because if you're making it, it's possible). So, the best thing to base your command around is to save time.
So, ask yourself, What do I do frequently that takes a long time to program in?
If you're not that much of the typical "hardcore programmer" (you're using batch here, so not many of you will match that term, it was purely to get my point across), then you might not need a function to save time, because you don't do that much.
In that case, ask yourself something along the lines of Which command lacks something?
That basically means, what command are you not happy with and you think you can do better?
Unfortunately, you can't just modify the code within the commands, so you have to resort to making a new command (such as SETX).
I probably should have explained how the command line (cmd.exe) parses commands. Well, looks like I found my topic for the next section.
Step 2: How Does Cmd.exe Parse Commands?
So, the first thing cmd does is search for a file named whatever the command (capitalization doesn't matter) was (let's use echo for this situation), with the extension of either .exe, .bat, .cmd or .com (I'm pretty sure there is either less or more extensions that it searches for). Once the file is found, whatever you put after it is sent to the script in the form of a parameter (these will be explained below).
So, if I put in a fictional command such as "EAT hamburger fries", and there is a file named EAT.bat located in the appropriate folder (that will be explained later too), then it send the words "hamburger" and "fries" as parameters 1 and 2 (respectively).
Let's say this is the code in the file EAT.bat
@echo %USERNAME% ate %1 and %2
Very short code, isn't it. Anyway, if I wrote "EAT hamburger fries" then the output would be
Prof Pickle ate hamburger and fries
It's a modern masterpiece, isn't it? If you're wondering where the variable "USERNAME" came from, it's a pre-set variable that holds the currently logged in user's username. Hint: To find all the pre-set variables and their values, type "SET" in the command line window.
As you may have guessed already, there can only be 10 parameters*. Wait, did he make a typo?
You heard correctly, there are 10 parameters: %1, %2, %3, %4, %5, %6, %7, %8, %9 and a special, pre-set parameter by the name of %0. This special parameter holds the file path of the file.
Here's an experiment for you to try:
- Create a new folder in your Documents named Test (if there isn't one already)
- Open up Notepad and type in the following...
3. Save it as "parameters_test_01.bat" (you can save it as whatever you want, It won't make any significant difference
4. As you may have already guessed, you now need to double click on it.
What you should see is the file path of the file echoed on the screen. Fancy, huh?
I do think that about sums it up (knowing me I'll leave out some vital piece of knowledge that will determine the success of failure of your life).
* There can be more than 9 user-set parameters, but you can only access 9 of them. Using the shift command, you can move the value of a parameter to the parameter before it. Here's an experiment you can try...
echo Press any key to send the following parameters to a command:
echo 1 2 3 4 5 6 7 8 9 10
call :command 1 2 3 4 5 6 7 8 9 10
echo The script has returned from the command.
echo Press any key to exit
echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
echo The shift command will now be used to shift the values.
echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
Step 3: Let's Actually Do Something!
So, first you need to decide on the syntax of your command. I will be making a command to echo a random number between parameter one (max) and parameter two (min).
So, here's the code:
if [%1] equ [/?] (
echo Echoes a random number.
echo Usage: RANDOM max min
echo "max" in the maximum number and "min" is the minimum.
echo Max cannot exceed 32767.
exit /b 0
if %min% geq %max% (
echo Minimum number is less than or equal to maximum number.
exit /b 1
set /a num=%random% %% %max% + %min%
So, what's so useful about a command that only echoes the result?
Well, so long as a code has an output, you can set it as a variable. So, here is a little "program" I made up.
@echo off & setlocal enabledelayedexpansion
echo Welcome to Prof Pickle's amazing game of luck!
echo Just press any key to roll the dice and try to get a 7!
echo Press any key to start!
if defined val echo Last role: %val%
echo Pres any key to roll the dice.
for /f %%i in ('random 6 1') do set dice1=%%i
for /f %%i in ('random 6 1') do set dice2=%%i
set /a val=dice1+dice2
if %val% neq 7 (echo LOSER!) else echo WINNER!
echo Dice #1 - %dice1%
echo Dice #2 - %dice2%
echo Total - %val%
echo Press any key to play again
So, that game isn't going to win any Academy awards, but it's just an example. So, what will you do to utilize your new command?
Wait! Not so fast. Before you use your command, you need any batch file (including the command prompt itself), you need to make it accessible. Keep reading to find out.
Step 4: Installation
Ok, so you have your command, but how do you use it? Well, you need to "install" it on your computer. You can make an auto installer, but you need to have admin permission. The basic gist is you copy the file from your USB into their Windows System32 folder.
So, either you can make an auto installer (which I won't be doing), or you can drag and drop your file into your C:\Windows\System32 folder within your computer.
Note: If you make a script that is intended to be used by more than yourself, you should put your new command in the same folder as the script, that way it will still work.*
* It will work so long as it's in the current directory.
So, I think that about wraps it up.
If you're looking for some pre-made functions (made by me), see https://www.instructables.com/id/Dos-Functions/