Part 1, The Basics:
In each of these these tutorials I will first saw you the code and then go over them line by line.
So here is our first script.
@echo off
@echo Hello World
pause
Here we start off with a very, very simple script.
So the first line is...
@echo off
Basically this will stop the Command line from displaying the your source code and just show
the correct output.
Echo on
Echo off
See the different?
You don't want want your script lookin ugly.
Note: Having echo on can be useful when your debugging bigger scripts.
Next is a echo command
echo Hello world!
Basically echo will display whatever string you give it on the screen.
(A string is just a bunch of letters "Stringed" together, in the above example "Hello World" is the string)
Pretty easy huh? :)
Saving and Running your scripts.
When your saving your scripts make sure you choose "All files" from file type, and you give it the extension .bat.
Example Helloworld.bat.
To run your script all you have to do is click on your .bat file in the folder where you saved it
Or you can CD to where you saved the file in the command line.
Example cd C:\Scripts\Batch (Or where ever you saved it) and type Helloworld.bat
If you have a more urgent message that you need to display to the user you can use the
msg command.
msg * Hello world! Display boxes for the win!
Variables
Variables will be used more then anything else in your scripts so knowing how to delare them
is important.
So here is how you do it
set > variable=Helloworld!
set variable1=Hello
set variable2=World
echo %variable1% %variable2%
After setting variable1 and 2 you can then print them on the screen just like you do strings
only you in case the variables in % otherwise it will print Variable1 instead of the value of the variable.
GOTO
The GOTO is very useful for creating loops.
To use this command you have to have a "Marker" set it place.
Example
sea /a count=0
:loop #This is the marker
set /a count=%counter%+1
print count
pause
GOTO :loop
First we declare count and give it the value of zero.
Then we give it the marker, Its just a string followed by a Colon( : )
The next line add 1 to the variable count, We print it out, pause it and then we use the GOTO command to go back up to :loop and redo the whole process.
Note: This script will go in an Infinite loop until you stop it(CTRL+C or Quit the window)
Thats all for now!
I hope you enjoyed this and that you learned sometime, I will have a follow up post soon with more advanced stuff.
So check back soon!