Introduction: How to Create Your Own Commands With Batch

About: I enjoy anything to do with computers. I have made some interesting scripts, and I am constantly improving my skill set. One day I hope to become a programmer, although in what category is still a mystery (al…
--EDIT1--
Corrected incorrect information on step 2 (to do with limitations with parameters).

--EDIT2--
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.


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?

This section basically covers what happens when you put something like "echo Hello". Unfortunately (I use that word too much), I don't know the code they use for it, so I can't exactly be perfectly accurate with this. Besides, this is just to demonstrate what happens when you type in a command (you probably shouldn't skip this if you don't know already). 

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:
  1. Create a new folder in your Documents named Test (if there isn't one already)
  2. Open up Notepad and type in the following...
@echo off
echo %0
pause

   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 off
echo Press any key to send the following parameters to a command:
echo 1 2 3 4 5 6 7 8 9 10
pause>nul
call :command 1 2 3 4 5 6 7 8 9 10
echo.
echo The script has returned from the command.
echo.
echo Press any key to exit
pause>nul
exit

:command
echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
echo.
echo The shift command will now be used to shift the values.
shift
echo.
echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof



Step 3: Let's Actually Do Something!

Now's the time to 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:

@setlocal enabledelayedexpansion
@echo off

if [%1] equ [/?] (
echo.
echo Echoes a random number.
echo.
echo Usage: RANDOM max min
echo.
echo "max" in the maximum number and "min" is the minimum.
echo Max cannot exceed 32767.
echo.
endlocal
exit /b 0
)

set max=%1
set min=%2

if %min% geq %max% (
echo.
echo Minimum number is less than or equal to maximum number.
echo.
endlocal
exit /b 1
)

set max-=min
set /a num=%random% %% %max% + %min%
echo %num%
endlocal



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

:intro
cls
echo Welcome to Prof Pickle's amazing game of luck!
echo.
echo Just press any key to roll the dice and try to get a 7!
echo.
echo.
echo Press any key to start!
pause>nul

:game
cls
if defined val echo Last role: %val%
echo.
echo Pres any key to roll the dice.
pause>nul
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
cls
if %val% neq 7 (echo LOSER!) else echo WINNER!
echo.
echo Dice #1 - %dice1%
echo Dice #2 - %dice2%
echo Total - %val%
echo.
echo Press any key to play again
pause>nul
goto game


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.

NOTE:
If you're looking for some pre-made functions (made by me), see https://www.instructables.com/id/Dos-Functions/