In part 1 of “Scripting Your Own Computer Repair Tools”, I showed you how to create scripts that make use of certain applications. In part 2 I showed you how to make scripts to manipulate files. In part 3 we are going to cover the use of IF statements, GOTO’s and setting variables. To demonstrate all three we are going to make a mutli-choice menu that allows us to choose what our script does.
Disclaimer: This article is for people with a good understanding of Windows only. I highly, HIGHLY recommend that you do scripting in either a Virtual Machine environment or a dedicated, unimportant machine in case you make a mistake with one of your commands and do some damage to the system. Technibble or I cannot be held responsible if you do damage to the operating system or lose any data.
First, let me explain what IF statements and GOTO’s do:
“IF Statements” allow us to test whether a certain condition is true and if it is, perform a certain action. If its false, the script will continue onto the next line.
“GOTOs” tell the script to goto another location in the script with the “Label” you specify, ignoring all the code that was before that point. Labels are identified with a colon in front of them.
Here is an example of me using both. I have spaced these out so it is more readable, but this still is a single script:
@ECHO OFF
IF "%USERNAME%"==Bryce GOTO CORRECT
:WRONG
ECHO Your Windows Username is NOT Bryce! It is "%USERNAME%"
GOTO END
:CORRECT
ECHO Your Windows Username IS Bryce!
:END
@PAUSE
Lets break that code down:
@ECHO OFF – This tells the script not to show the word “ECHO” when it says whether the answer was wrong or right. Just show the text after it.
IF %USERNAME%==Bryce GOTO CORRECT – We have used the system variable %USERNAME% in our past examples and it contains the username of the person who is currently logged onto the computer. The IF statement asks if the username equals “Bryce”. If it does, then GOTO the location called “:CORRECT”.
If the username is NOT Bryce, then the script will continue down the page. The script always works its way down the code unless told otherwise.
:WRONG – This is what the code does if the username is NOT Bryce. In this example we have told it to ECHO (meaning print on the screen) that the username is not Bryce and tell what the username actually is. Windows will fill out the username variable with what the username really is.
After it tells the user what the username is, it says GOTO END. Now, remember how I said that the script will work its way down the code unless told otherwise? If I don’t have this “GOTO END” part, the script will continue to run down the page and display the correct answer as well, which we don’t want.
So, I placed an :END label at the end of the script and told the script to GOTO END which “jumps” the :CORRECT part and thus the correct text never shows.
:CORRECT – This part is similar to the :WRONG part except that it says the answer is correct. This part doesnt need a GOTO END part like WRONG had because there is nothing below it. It is not a problem when the script continues down the page since it will end anyway.
@PAUSE – This just pauses the script and prevents it from closing the command window so we can see our results.
Setting Variables
As mentioned in the previous articles, variables are values that change. Variables like %USERNAME% are system variables that are set by Windows. However, we can actually set and call upon our own variables. Lets start a new script and create our own.
@ECHO OFF
SET XX=Fish
ECHO %XX% And Chips!
@PAUSE
In this example we set the XX variable to “Fish”. We then tell the computer to print out “%XX% And Chips”. Since the XX variable has been set to “Fish”, it will print out “Fish And Chips!”
We can also set variables on the fly based on what the user typed in using the /P switch (which stands for prompt). In a new script, add the following:
@ECHO OFF
SET /P XX=Type a number, then press ENTER:
ECHO You Pressed %XX%
@PAUSE
Anything the user types after “Type a number, then press ENTER:” will become the value of the variable. If you type 8, %XX% will now carry the value 8.
Now, Lets Put It To Good Use. Lets Create a Menu!
Create a new script and add the following:
@ECHO OFF
:MENU
ECHO.
ECHO Choose a Door:
ECHO 1 - Door Number 1
ECHO 2 - Door Number 2
ECHO 3 - Door Number 3
ECHO 4 - Exit the game
SET /P XX=Type 1, 2, 3 or 4, then press ENTER:
IF %XX% ==1 GOTO DOOR1
IF %XX% ==2 GOTO DOOR2
IF %XX% ==3 GOTO DOOR3
IF %XX% ==4 GOTO EOF
:DOOR1
ECHO You chose Door Number 1 and fell into the snake pit. You are dead.
GOTO MENU
:DOOR2
ECHO You chose Door Number 2 which is the path to the outside. You are free!
GOTO MENU
:DOOR3
ECHO You chose Door Number 3 and there was a Lion behind it. You are dead.
GOTO MENU
In this example, we used ECHO to tell the user what the options are so they know what to choose. We then then set the variable based on what the user typed with SET /P XX.
Once the variable has been set, we check what it was set to with the IF statement. We will assume that the user pressed 2 so IF %XX% was set to 2, GOTO DOOR2.
The script then jumps to the :DOOR2 label and runs the code in there. In this example it prints “You chose Door Number 2 which is the path to the outside. You are free!”.
To prevent the script from running the DOOR3 code, we told the script to go back to the MENU label which is at the top of our script above the menu options.
The script will then work its way down the script again running the menu code and asking us which door to choose again.
The only way to get out of this loop is to choose option 4 which says GOTO EOF. “EOF” stands for “End Of File” and it will close the script down.
Lets Make a Technician Tool
Now that you know how to make a basic menu, lets make a technician tool.
If we choose option 1 it will fetch the computers mail settings and CD keys (which we learned in Part 1). This part requires produkey.exe and mailpv.exe in the same directory.
If we choose option 2 it will copy the contents of the current users Desktop to the folder the script is currently running from (which we learned in Part 2).
If we choose option 3 it will display the currently logged on users username (learned in Part 1)
If we choose option 4 it will close the script.
In a new script, NOT located on the Desktop, add the following:
@ECHO OFF
:MENU
ECHO.
ECHO What do you want to do?:
ECHO 1 - Collect Mail Settings and CDKeys (needs produkey.exe and mailpv.exe in same folder)
ECHO 2 - Copy the current users Desktop folder
ECHO 3 - Display the currently logged on Username
ECHO 4 - Exit
SET /P XX=Type 1, 2, 3 or 4, then press ENTER:
IF %XX% ==1 GOTO KEYS
IF %XX% ==2 GOTO COPYDESKTOP
IF %XX% ==3 GOTO LOGGEDON
IF %XX% ==4 GOTO EOF
:KEYS
produkey.exe /stext "%COMPUTERNAME%\Keys.txt"
mailpv.exe /stext "%COMPUTERNAME%\Mail.txt"
GOTO MENU
:COPYDESKTOP
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
GOTO MENU
:LOGGEDON
ECHO The currently logged on user is "%USERNAME%"
GOTO MENU
I kept this example fairly simple so it is easy to follow but you can imagine the possibilities with the script like this. I have created a zip file which you can download here containing all of these examples we have talked about if you need help or just want to keep the samples for reference.
In Part 4, Ill show you some handy applications you can make based off what we have learned.
In the mean time, don’t forget that Technibble has a rich scripting community on the forums with a handful of scripting gurus in there. The scripting forum isn’t visible to those not logged in, been on the forums for a few days and made a few posts since we don’t want them to be visible to Google and non-technicians.
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.
hey thx A LOT Bryce for this article ! this is gold bro gold ! always a pleasure to read year weakly articles !
Desde Venezuela BIG Thx man ! keep it up !
Very nice article series, thanks and we’d love to see more too!
Nice post there Bryce. I would of mentioned the ‘set’ command in this article as well since you are talking about variables and using the ‘if’ and ‘goto’ statements.
I wonder how many segments you are going .. Hoping I’m goin’ to learn somethin’ new soon =)
I love this “You chose Door Number 3 and there was a Lion behind it. You are dead.” It reminds me so much of the classic PC games I used to play and how games today lack that humor. Oh yeah and nice job too with the actual article.
In the segment titled “Lets Make a Technician Tool”, there is the following snippet
from the code block presented.
IF %XX% ==1 GOTO KEYS
IF %XX% ==2 GOTO COPYDESKTOP
IF %XX% ==3 GOTO LOGGEDON
IF %XX% ==4 GOTO EOF
The 4th IF should NOT be there, only the “GOTO EOF”
The Prevents a non-menu choice from “FALLING” into the KEYS section of the script.
Another alternative would be to echo an error message complaining about ‘bad’ input, and loop back to re-display the menu.
The point is: make sure the execution control commands completely do their job and only exec the intended portions of the script.
Some things to take into consideration..
Where Bryce has:
IF %USERNAME%==Bryce GOTO CORRECT
This is not the same as:
IF %USERNAME%=="Bryce" GOTO CORRECT
Or is not the same as:
IF "%USERNAME%"==Bryce GOTO CORRECT
However, this will work (note the quotes)..
IF "%USERNAME%"=="Bryce" GOTO CORRECT
One other thing that trips people up is the following:
IF %USERNAME%==bryce GOTO CORRECT
Because: Bryce is not bryce nor is it BRYCE..
On way to get around this is to use an i switch so it does not matter..
IF /I %USERNAME%==bryce GOTO CORRECT
Dropping to a command-line and we do a if /? we see the following:
and the /I switch, if specified, says to do case insensitive string
compares. The /I switch can also be used on the string1==string2 form
of IF. These comparisons are generic, in that if both string1 and
string2 are both comprised of all numeric digits, then the strings are
converted to numbers and a numeric comparison is performed.
Another thing you can do is where Bryce has (note: the double equals ==):
IF %USERNAME%==Bryce GOTO CORRECT
You can update it to:
IF %USERNAME% EQU Bryce GOTO CORRECT
Or rather:
IF %USERNAME% NEQ Bryce GOTO CORRECT
Of course, you can use:
IF 1 LSS 2 GOTO CORRECT
Or..
IF 2 GTR 1 GOTO CORRECT
HTH,
Kent
Thanks for posting this as i’m really finding the whole series very interesting. Keep ’em coming.
I was wondering how to get the backup script to back all accounts on a customers computer instead of just the account that is currently logged in.
Great articles on scripting I have gotten so much from it so far.
Thanks
James