February 24th, 2002, 10:48 PM
Yet Another Batch File Tutorial
I wrote this ages ago, when I first joined AntiOnline. I have seen other posts regarding batch files, but are they this pretty?
Introductory Note: If I say anything that is hilariously obvious, I'm extremely sorry.
Batch files are basically the same as shell scripts but different commands, obviously. They are meant to save you time so you don't have to keep performing the same action over and over again but they can also be constructed to do so much more (within reason), like test for vulnerabilities in a server, DoS someone or even become a lethal virus or trojan. Seeing as we don't want it to do any of these, except maybe the first, we'll take it slowly.
Here is a sample "Hello World" program:
**************** helloworld.bat *************
echo Hello World
Basically what that does is clear the screen (cls) and print out "Hello World". The @echo off is so that you don't see the actual commands themselves and the command prompt. Try it with and without the @echo off and you will see what I mean.
Now save all that as helloworld.bat and either double-click it or call it from the command prompt: "helloworld.bat" or "helloworld" will both do, when you are calling it from the prompt.
Batch files are really easy and useful as long as you have a list of interesting commands. Batch files can do anything from test vulnerabilities in servers (like is there an anonymous login?) to extremely dangerous virii to even solve world hunger. But I think batch files were orginally intended to move files and create directories. Oh well, welcome to the 21st century.
Here is another program:
It can do a load of stuff. It can even test for an anonymous login through FTP. If you want it to do that, when you are calling the program from the command prompt enter in a server after the name. For example: "jethbat.bat www.myserver.com"
******************** jethbat.bat *****************************
rem This is a remark.. eh.. hello?
echo Welcome to the Amazing Program for all you Crayzee Catz
echo Where do you want to go today? (tm)
echo 1) Give me a list of all my screensavers
echo 2) Test out the server (%1) for anonymous logins
echo 3) Send Jethro an email
echo 4) Go to AntiOnline.com
echo 5) Give me a list of open ports
echo 6) Tell me my IP address
echo 7) Tell me what Windows version I am using
echo 8) Open up my Windows Directory
echo 9) Create a directory on my desktop and call it 'jethro'
choice /c:123456789 Make your selection now:
IF ERRORLEVEL 9 goto createdir
IF ERRORLEVEL 8 goto openwindows
IF ERRORLEVEL 7 goto winversion
IF ERRORLEVEL 6 goto tellmemyip
IF ERRORLEVEL 5 goto openports
IF ERRORLEVEL 4 goto antionline
IF ERRORLEVEL 3 goto emailjethro
IF ERRORLEVEL 2 goto testserver
IF ERRORLEVEL 1 goto screensavers
dir C:\*.scr /B /S /W
echo open %1>C:\testserver.log
winipcfg /batch C:\winipcfg.000
IF EXIST C:\WINDOWS\Desktop\jethro\*.* goto end
echo Jethro Folder > C:\WINDOWS\Desktop\jethro\jethro.txt
echo Thank you for using my program
echo The end
There are a number of commands here you might not have come across. I will do my best to go through any possible new commands here.
REM - This is where you can put in a command. Like the or the // C++ comment and the ' BASIC comment.
ECHO. - This prints out a blank line
CHOICE - This gives you an INPUT prompt with a number of choices. For example, used with the /c:123456789 attribute you can have the choices 1,2,3,4,5,6,7,8 or 9. If I had used /c:abzqrst3291, your options would have been a,b,z,q,r,s,t,3,2,9 or 1. If you leave the /c switch out your options are either y or n. The program continues when they input one of the options.
IF - Anyone who has any experience with any language will know what this is. The IF conditional statement is like a universal command. It can take many forms such as "IF (variable == value) command". In MS-DOS it takes the more BASIC form. "IF %variable% == value COMMAND". But the IF doesn't have to be like this. There are many other ways of using the if. Like in the following:
ERRORLEVEL - The choice command returns a value to the batch file when it is finished. This is called the errorlevel. Many programs (mostly the older ones) do this, if they are meant to help you in batch files. The errorlevel after the choice command, tells us which option was chosen. For example, if the errorlevel is 2, that means the second option was chosen. If the errorlevel was 8, that means the eigth option was chosen.
For example, if I had used the choice command with the switch /c:abc, the chosen option was the either going to be the first (a) the second (b) or the third (c) option. So if the errorlevel came back as 3 that would mean that the third option had been chosen: c. God, I'm sure there is a way to condense that explanation :).
(NOTE: When you are using the IF ERRORLEVEL N command thing, you must go in descending order, meaning the highest number first, down to the lowest number last. Notice the way I started with IF ERRORLEVEL 9 and moved down to IF ERRORLEVEL 1. You must do this, because if you start from IF ERRORLEVEL 1, the first condition will just be chosen and that would defy the whole point)
GOTO - The goto command is on many languages. Some programmers for some reason don't like this option, but I do. Basically, it sends the program to another part of the code. Usually only used with conditional statements like IF.
For example in the following line: "IF ERRORLEVEL 9 goto createdir", I wanted it to go to a label called "createdir" if the ninth option had been chosen.
If it had proceeded with the program, it would have gone to "IF ERRORLEVEL 8" but I didn't want that. I wanted to move it a label called "createdir" there it would make a folder on the desktop. When you are creating a label you must have it in the ":name" format but when you are sending the program to the label you only need to refer to it by the name, without the colon ":".
In "createdir" it checks if there is a folder on the desktop called "jethro" and if there isn't it makes one. At the end of the label, I didn't want it to continue so I sent it to another label, called ":end". All the labels, in this program, when they are finished, get sent to this label, because here it just gives a little message saying "Thank You" and it exits.
GOTO end - I had that at the bottom of all the errorlevel things so that if none of the conditions were met, I don't know how that would happen but anyway, it would go to the end label.
dir C:\*.scr /B /S /W - You are probably all aquainted with the dir command. It is like the "ls" command in UNIX. It gives you a list of all the files in the current directory. I made it find any *.scr files in the C:\ directory. I added three switches with it:
/B - This is so that is just gives the Bare neccesities (of life :). The name and nothing else, no modification date or size.
/S - This is so that it checks all the Subfolders: /WINDOWS, /PROGRA~1, all of them. When used in the C:\ directory, it effectievely searches the whole computer.
/W - This gives you the list in Wild list format, ie. in columns.
echo open %1>C:\testserver.log - There are a couple of things in action here. First off all, we ECHO the message "open %1". %1 is substituted for anything you may have entered after jethbat.bat if you called it from the command prompt.
For example, in the command prompt, if you had called jethbat with the command: "jethbat.bat", %1 would just be a blank space. If you had entered: "jethbat.bat [servername]", %1 would be subsituted for the server name.
If you had entered "jethbat.bat christmas tree shiny lights", %1 would have been substituted for "christmas." %2 would equal tree, %3=shiny and %4=lights. %0 would be jethbat.bat or if you had left out the .bat part (which you can do) it would just be "jethbat". But for the sake of this example, I have only included the %1 variable.
But why wasn't "open %1" printed out to the screen? Well my inquistive friend, it's because I have included the ">" redirection. This means that the echo will be put into a NEW file called C:\testserver.log. In the following two lines, I have the ">" as a double, ">>" because this means that it is to APPEND to the named file, not create a new file.
FTP -s:C:\testserver.log - This fires up the FTP program using C:\testserver.log as it's command sequence. C:\testserver.log contains instructions for it to try and FTP to the server %1, use an anonymous login name and use a Yahoo! email address. You will see if it has worked. Of course you could do this yourself very easily, but think of the seconds you are saving by running this batch file. Astounding.
DEL C:\testserver.log - Destroy the evidence :). This command simply deletes the file. No questions asked. If you want there two be an "Are you sure prompt?" after this command, just use the /P switch with the command.
(NOTE: Any file deleted in MS-DOS is not sent to the Recycle Bin, it is completely removed altogether!)
START - This command opens any directory or file in it's default program, specified in the registry. For example a mailto: address will be opened in Outlook (by default that is, unless you have changed it), http:// will be opened in your default browser, a folder will be opened in Windows...etc
Netstat - If you don't know what netstat is... Basically netstat tells you a load of stuff about your connections. With the -a it gives you a list of open ports. Time to pray and hope to God you don't have a Trojan :)
winipcfg - This gives you information basically about your internet protocol thing. Used with the /batch winipcfg.000 switch it saves all that to a file.
type winipcfg.000 - This types out all the information in winipcfg.000 to the screen.
ver - This gives you the version of your operating system.
IF EXIST - This checks whether a file exists or whether it doesn't. If it does and the condition is met, I have it go to the end, because there is no point trying to make a new folder that already exists. I have it check for any *.* existing file in the jethro folder and if there aren't, I just take it for granted that there is no jethro folder there and I make one (and place a file in it, so that it won't be just an empty folder, if you run the program again it will skip trying to make one).
md - This creates a directory.
pause - This gives the prompt "Press any key to continue...".
("where's the any key" - Quote courtsy of Homer Simpson).
That's what the program does anyway...
- Some other useful commands
rd - This deletes an EMPTY folder
deltree - This just deletes the folder, empty or not. /Y supresses the "are you sure?" prompt.
format - Format a drive
cd - Change the working directory.
time - Gives you the time
date - Gives you the date
prompt [message] - Changes the prompt. $p$g means the it gives the working directory and a >. But you can use things like "prompt ->" if you want it to look like one of those computer terminals that you see in the movies and jut use the CD command spontaneously to see what dir you are in.
CHDIR - CD
MKDIR - MD
RMDIR - RD
EXIT - Quit dos
MEM - Memory Information
[Drive] - Move to a drive. The CD doesn't work if you want to move to a completely different drive altogether.
EDIT - This opens MS-DOS' lovely text editor edit.com
COPY [file] [destination] - Copy a file to a new directory
MOVE [file] [destination] - Move a file to a new directory
RENAME [oldfilename] [newfilename] - Renames a file
REN - RENAME
TELNET [server] [port] - Telnets into a server through a specified port
ATTRIB - Displays or changes file attributes. Use the /? switch for help.
That's all I can think of at the moment, but there are literally tons if you are just willing to look for them. I have never been great at explaining things, hence the length of this tutorial which could probably be condensed into a much smaller size.
February 24th, 2002, 10:50 PM
You might wanna sort out them smilies.
February 24th, 2002, 10:51 PM
Yeah I sorted them out just then 30 seconds after I posted it. Jesus, you are amazingly quick
Originally posted here by cmnoop
You might wanna sort out them smilies.
February 24th, 2002, 10:53 PM
Speed is required at times!
February 24th, 2002, 10:56 PM
nice prog but I have a little remark:
your prog only works properly in WIN9x / ME cause winipcfg is not the proper command in Win NT / Win2K, for Win NT4.0 and Win2K it's ipconfig.
February 24th, 2002, 10:59 PM
February 24th, 2002, 11:05 PM
However Jethro nice tutorial....
February 24th, 2002, 11:38 PM
Thanks for the compliment. Yeah, I had forgotten that WINIPCFG wouldn't work on other systems, ME clouds the brain.
February 24th, 2002, 11:44 PM
*Note* the choice command does not work with 2000.
A squirrel with no nuts will soon starve.
February 24th, 2002, 11:52 PM