Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cyber's tutorial is a very good start for people but I'd like to look at some other commands people can use in their codes for a greater range of possibility.
- [size=15][color=red][b]The choice command:[/b][/color][/size]
- [color=red][spoiler]The choice command was introduced I believe in Windows XP XP3 or Windows Vista...
- What's good about the choice command?
- We've all made menus using:
- [code]
- set /p a=
- if %a%==y goto yes
- if %a%==n goto no
- [/code]
- But this requires the user to press enter every time which, for me, is annoying.
- The choice command eliminates the requirement to press enter.
- A basic choice section would look like this:
- [code]
- choice /n (List letters to use as choice without brackets such as YN) /M Text to display here (Such as Would you like another card in blackjack?)
- if errorlevel 2 goto x
- if errorlevel 1 goto y
- [/code]
- (it's important that it goes from highest errorlevel to lowest errorlevel)
- So you may or may not have seen choice command before, so let's go through each part.
- choice /n YNM /m Would you like to continue?
- if errorlevel 3 goto m
- if errorlevel 2 goto n
- if errorlevel 1 goto y
- Choice is the command /n is the switch to specify your own list of choices. If /n is not used, the default list of "YN" will be used.
- Lists are not seperated from anything and the list can be, I believe, as long as you want (as long as the characters can be read in batch xP)
- The /m switch is for adding your own text to display. In this case, I put "Would you like to continue?"
- On the prompt, that line will look like "Would you like to continue? (Y/N/M)"
- If a letter not listed in the brackets is pressed, an annoying beep is sounded which is why some people dislike using the choice command.
- What are the "if errorlevel x goto z" lines for? These are the parts that do a certain command depending on what the user types.
- How this works:
- If M is pressed in the previous example, an errorlevel 3 is returned.
- Errorlevel 3 counts as errorlevel 3 and every errorlevel below (so 2 and 1) which is why the list must be from highest to lowest errorlevel.
- How do I know pressing M returns errorlevel 3? The errorlevel returned depends on what number in the list of choices is the choice of the user.
- Y N M
- 1 2 3
- Y was the first choice listed, N was the second choice listed and M was the third choice listed and they have their respective errorlevels.
- [u]KEEP IN MIND: Choice command is not present on all systems (common on vista or higher systems) so be careful when making batch scripts using choice. Not everyone will be able to use it.[/u]
- [/spoiler][/color]
- [size=15][color=fuchsia][b]The For command:[/b][/color][/size]
- [color=fuchsia][spoiler]I'm not an expert with the for command, I haven't done any serious work with it and maybe gulf or spin (or cyber) could help out with this one.
- The basic for command basically sets a variable to change to everything in a set and do commands.
- A simple for command would be as such:
- [code]
- for %%G in (G,L,1,T,C,H,3,D,i,s,A,W,E,S,O,M,E) do echo %%G
- [/code]
- will echo the variable %%G which is changing to every letter I listed in the set.
- The characters listed in the set don't have to only be single digits...
- It can be words too, such as
- [code]
- for %%H in (Hello, World, My, Name, Is, GL1TCH3D) do echo %%H
- [/code]
- Another simple For line would look like:
- [code]
- for /L %%J in (1,1,10) do echo %%J
- [/code]
- This time we used the "/L" switch which is used to make a "counter".
- In this example, it echos 1,2,3,4,5,6,7,8,9,10
- Notice how I didn't list all those in the set though?
- Using the /L switch makes the set act as such:
- (starting number,step (how much is added or subtracted from the starting number), ending number)
- It could also have gone as such:
- [code]
- for /L %%J in (10,-1,1) do echo %%J
- [/code]
- Which will echo
- 10,9,8,7,6,5,4,3,2,1
- [u]Options: [/u] Options are a bit harder to understand (at least for me, when compared to the other commands).
- Options allow you to take parts of a text in a for loop. For some it's useful but I still haven't used it for anything (yet).
- The command is in the form:
- [code]
- for /f "options" %%A in (set) do command
- [/code]
- Where the /f switch is to signify options, "options" are the options you set and (set) is the set you want to use...
- (set can be a command, file or string)
- Some options are:
- Eol: End of line character. Set the character which signifies the end of a line. If the eol is the first character in the line, the entire line is skipped.
- Skip: skip the number of lines set from the beginning of the set.
- Delims: set the character which signifies the end of a part.
- Tokens: set parts of the set as variables. Parts are sperated by spaces or delims.
- So let's start with an example:
- You have a text file named file1.txt with contents:
- [code]
- Hello, my name is GL1TCH3D
- :It's nice to meet you
- P.S: I'm awesome
- [/code]
- and a batch containing
- [code]
- @echo off
- For /f "eol=: delims=, " %%a in (file1.txt) do echo %%a
- Pause
- [/code]
- Running this batch file should show:
- [code]
- Hello
- P.S:
- [/code]
- notice how nothing after "hello" is present? That's the work of delims. Since delims was set to the comma and there's only 1 token by default, everything after the comma is dropped. Also, you'll notice the second line from the text is missing because eol was set to remove lines starting with :
- Now let's try using different options...
- [code]
- @echo off
- For /f "skip=1 tokens=1,2,3 " %%a in (file1.txt) do echo %%a bleh %%b %%c
- Pause
- [/code]
- Notice I changed the options and the command.
- This should show you
- [code]
- :It's bleh nice to
- P.S: bleh I'm awesome
- [/code]
- notice my first option is "skip=1" which skips the first line of my set.
- My second option is tokens=1,2,3
- Tokens sets new variables.
- Token=1 sets the original variable (in this case %%a) as the first part of the line.
- In the above example, that's :It's and P.S: respectively.
- The token=1,2,3 sets variable1=part1, variable2=part2 and variable3=part3
- Variables always go up by one, so if the first variable is %%h then variable2 is %%i
- Echo %%a bleh %%b %%c after tokens=1,2,3 gives you:
- Part1 bleh part2 part3
- If you have trouble with these concepts you can PM me or add me on skype for more examples.
- [/spoiler][/color]
- [size=15][color=green][b]The setlocal enabledelayedexpansion:[/b][/color][/size]
- [color=green][spoiler]
- For most people, this won't be used much, though it's good to know about it (I wish I knew about it while making my blackjack xd).
- setlocal enabledelayedexpansion is harder to explain...
- One example script:
- [code]
- @echo off
- setlocal enabledelayedexpansion
- for /l %%L in (1,1,9) do (
- set awesome%%L=%%L
- echo !awesome%%L!
- )
- [/code]
- This will echo 1,2,3,4,5,6,7,8,9 and set variables awesome1-9=1-9
- If setlocal enabledelayedexpansion was off and instead of !awesome%%L! I used %awesome%%L% the prompt would not recognize "awesome%%L" as a variable. Though, with setlocal enabledelayedexpansion, you can call a variable using !variable! which will make the prompt wait for variables inside variables to expand.
- setlocal enabledelayedexpansion is useful for setting and using many variables.
- [/spoiler][/color]
- [size=15][color=cyan][b]The "part variables":[/b][/color][/size]
- [color=cyan][spoiler]
- Part variables are used to take a part of a variable.
- An example:
- [code]
- @echo off
- set x=1234567
- echo %x%
- echo %x:~0,1%
- echo %x:~0,2%
- echo %x:~0,5%
- pause
- [/code]
- See how the part variable takes a part of a variable that has already been set?
- It doesn't necessarily have to be %x:~0,1%
- It could be %x:~3,1%
- [/spoiler][/color]
- [size=15][color=yellow][b]The Directors:[/b][/color][/size]
- [color=yellow][spoiler]
- You've probably seen these and already understand these, but the
- > and >> commands.
- You have a regular command, say
- echo Hello World!
- When the prompt reaches this point, it will display "Hello World!" on the prompt.
- Now, echo Hello World! > Hello.txt
- When the prompt reaches this point, nothing will be shown, though, a text file will be created in the current directory, named "Hello.txt" with contents "Hello World!"
- Sound simple enough?
- Now, if you do that same line above, you'll find that Hello.txt will still only contain one line: Hello World!
- But you want it to contain 2 lines!
- So then you use the double, >> to "add" to the file, the single, > is used to overwrite or create a file.
- This is particularly useful when you want to make a batch with settings and such.
- A quick example would be:
- [code]
- @echo off
- echo taskkill /im cmd.exe /f >>cool.bat
- start cool.bat
- [/code]
- Will create a batch then start it. The new batch contains the line "taskkill /im cmd.exe /f"
- [/spoiler][/color]
- [size=15][color=salmon][b]The Call command:[/b][/color][/size]
- [color=salmon][spoiler]
- The call command is used to add the commands from one batch into the batch that is calling it.
- You can also set parameters using the call command, but more on that later.
- Let's say I have batch 1 and batch 2
- 1: [code]
- @echo off
- call 2.bat
- echo %x%
- pause
- [/code]
- 2:[code]
- set x=This is awesome
- [/code]
- When batch one is opened, it hits the "call 2.bat" line, which opens the second batch as if it were part of the first batch, which will set x=This is awesome.
- Now the first batch will echo %x% which will display "this is awesome"
- This can be used if you're making a batch game to save HP.
- [code]
- @echo off
- if exist settings.bat goto continue
- set HP=1000
- echo Your current hitpoints are %HP%.
- echo set HP=%HP%>>settings.bat
- :continue
- call settings.bat
- pause
- [/code]
- Call can also be used like the goto command with a little bonus.
- The regular goto looks like:
- [code]
- goto label
- [/code]
- or
- [code]
- goto :label
- [/code]
- So what can call do more?
- Call allows you to set parameters (this can be used when calling another batch too).
- call section/batch parameter1 parameter2 parameter3... parameter9
- An example code:
- [code]
- @echo off
- call :bleh hello bleh whatsup
- blahblahblah
- :bleh
- echo %1 %2 %3
- [/code]
- So let's go through this.
- call :bleh brings you to the label :bleh, skipping all the stuff between (just like goto)
- The "hello", "bleh" and "whatsup" are the parameters set with the call command. Parameters can be used using %1 - %9
- As you can see, I did "echo %1 %2 %3" which will show "hello bleh whatsup" in the cmd window, which are the parameters I set.
- This can be done when calling other batch files too.
- [/spoiler][/color]
- [u]Thanks for reading![/u]
- For more examples or explanation, feel free to add me on skype: GL1TCH3D
- As a little bonus, here's a code I made
- [spoiler]
- [code]
- @echo off
- setlocal enabledelayedexpansion
- choice /m Would you like to goto awesomeness?
- if errorlevel 2 exit
- echo PREPARE, FOR NOT HALO REACH!!!!
- echo set a=http:// >settings.bat
- echo set b=pastebin.com/ >>settings.bat
- echo set c=TVSvgBGW >>settings.bat
- call settings.bat
- set cool=%a%%b%%c%
- start /wait !cool!
- if errorlevel 0 (
- echo GL1TCH3D is pretty cool, no?
- )
- exit
- [/code]
- [/spoiler]
- [size=15][color=White][b]Other topics and commands:[/b][/color][/size]
- [spoiler]
- Here I'll be throwing in various things such as simpler commands previously explain in other tutorials just to give users a more rounded read if they need a recap on previous commands and such.
- Echo:
- The echo command has two functions. The first and most useful function is for the scripter to display information to the user. A typical line of code using echo looks like this:
- [code]
- Echo Hello
- [/code]
- This will display the word "Hello" to the user.
- You can use this in more advanced scripts to display important information to the user.
- There are many possibilities with this, here's an example:
- [code]
- @echo off
- echo Hello %username%
- echo How may I help you today?
- [/code]
- Of course this is not a complete script but it will show the user the following (using my own username as an example):
- [code]
- Hello GL1TCH3D
- How may I help you today?
- [/code]
- Now, if you're really not familiar with batch, you're wondering what that first line does, @echo off.
- Here we come to the second function of the echo command. By default, echo is on for batch. What does this mean?
- When echo is turned on, the command prompt will "echo" the command input before producing the output for the user.
- If we use the same code as above, but without turning off echo, you'd get the following (before it closes, of course):
- [code]
- *the current directory*> echo Hello GL1TCH3D
- Hello GL1TCH3D
- *the current directory*> echo How may I help you today?
- How may I help you today?
- [/code]
- Echo on/off not only affects the echo command, but most commands processed.
- Note: The @ sign at the beginning of a line will nullify the output of that line. This is the same as using >nul at the end of the line.
- Pause:
- The pause command is a very simple command used to stop the script from continuing until user input is received. A simple script to demonstrate this would be:
- [code]
- @echo off
- echo Hello
- pause
- [/code]
- Normally, the script would close when reaching the end. Though, the pause command will "pause" the script before it ends until the user enters any input.
- Ping:
- The ping command is used to send small packets of data to a chosen target for any reason you may see. (It could be to check if a website is up, etc.)
- Another purpose for the ping command that people are using it for is to do a short pause that does NOT require user input.
- The simple ping command is as follows:
- [code]ping -n # -l # ip address[/code]
- -n is the switch for number of pings to send (the number of packets to send to the target).
- -l is the packet size of each packet (in bytes, maximum is something like 60,000 I believe).
- # are just numbers you replace to complete the specified switches which I listed above.
- IP address is the hostname (such as google.com) or the actual IP (such as 174.193.43.14).
- Now how do we use this for making short pauses?
- Since pinging takes a bit of time, what general people do is they ping themselves (localhost or 1.1.1.1).
- The number of packets roughly corresponds to the time it pauses for (except when you send 1 packet, the pause is really short).
- So to pause for roughly (keep in mind I said roughly. It's not exactly 2 seconds) you would use something like:
- [code]
- @ping -n 2 localhost
- [/code]
- Notice I put @? That's to nullify the output of ping which is fairly large and not something you necessarily want to show to the user when using ping as a short pause.
- Now, I thought of a much more accurate way to pause for a desired amount of time which I'm surprised I've never seen before.
- One of the switches for ping is -w which stands for wait-time or time to wait for each packet to return before declaring the ping as a fail.
- The wait time is measured in milliseconds. Therefore, you can ping an imaginary IP (I use 1.2.3.4) which will never return a packet, and set the desired time to wait using the wait-time switch.
- An example of this would look like:
- [code]
- @ping -n 1 -w 2000 1.2.3.4
- [/code]
- I set the command to only send 1 packet since it's pointless if you send more and may change the time of completion of the entire command.
- This should take almost exactly 2 seconds for the command to complete, making it much more accurate than the previous method of waiting and making it more accessible unlike the wait command which you can download as an add-on.
- Set:
- The set command is one of the most important commands in batch. You'll rarely see a good script that does NOT use the set command.
- The set command has multiple functions. The basic function, requiring no switches, sets a value to a variable.
- A section of code to show this would be:
- [code]
- @echo off
- set x=Hello
- echo %x%
- @pause
- [/code]
- This will set the value "Hello" to the variable "x" so that when the variable is called upon, the value for the variable is given.
- In this case, the variable "x" is called upon in the echo command (which displays text to the user).
- So when "x" is called upon, the variable is replaced with its value, in this case "Hello".
- So what you're really doing is telling it to replace the variable "x" with its value "hello" then display that value to the user, producing the output "Hello".
- Why is this useful? In more complicated scripts, the set command can be used to create a check system.
- For example:
- [code]
- if "%time%"=="10:13:44.63" set true=1 else set true=0
- [/code]
- In this little line, the system will check the current time against a preset time. If the current time matches the preset time, then the variable "true" is given the value of "1". Otherwise, it is given the value "0". This can be used in a script to check the current time, among the infinite uses of the set command.
- Switches for set:
- Set has a few switches that people should be familiar with.
- The most useful (to me anyway) is the /p switch which is used to gain user input.
- The usage looks like:
- [code]
- set /p variable=Text to display to the user
- [/code]
- Example in a script:
- [code]
- @echo off
- echo Hello %username%
- set /p command=Please enter a command for me to perform:
- [/code]
- This will allow the user to set almost any string of ASCII text as the value of the variable "variable". The batch can continue on to use this value for other purposes.
- The biggest downside with this is the fact that the user needs to press enter after entering the value which may deter some scripts. For example, if you're making a game, pressing enter after every input can be annoying if there are a lot of inputs.
- One of the commands covered in this tutorial is the choice command which bypasses the need for "enter" but only allows one character to be input at a time.
- Another switch for set is the /a switch. This is not used as often and is used to tell the command prompt that an arithmetic operation is to be performed on the input.
- For example:
- [code]
- set /a a=1+1
- [/code]
- Normally, this would set the value "1+1" to the variable "a", but, because of the /a switch, the mathematical operation is performed and a value of "2" is given to the variable "a" instead of "1+1".
- Notes:
- Batch has many limitations when it comes to calculations. First of all, you're limited to a maximum number of 2^(32-1). Furthermore, batch does not process decimal numbers and will drop the decimal. Even more, batch will not perform operations such as factoral (!) and exponent (^). Myself, including others, have worked quite hard on making scripts to remedy these issues. For example, I made a script, which is almost complete, to take any 2 numbers (including decimals) and perform any basic operation on them to produce an answer with decimals.
- I've also made scripts that calculate exponents and factorals without using the operation signs. I was working with someone on bypassing the 32bit limitation on the numbers.
- [/spoiler]
- [size=30]Feel free to PM me or add me on skype about ANY batch related questions as I'm always happy to help.[/size]
Add Comment
Please, Sign In to add comment