Tuesday, December 18, 2007

Batch Tutorial

This is a tutorial for Dos Batch, If you like it or if you found it helpful please leave a comment.

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 On

Echo off
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.
Saving your file


To run your script all you have to do is click on your .bat file in the folder where you saved it
Opening the file

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.

Variables

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!

Python links

Here are some good links for all the Python programmers, Enjoy

AwareTek
Tons of Python Tutorials and even a Podcast all about Python

Pygame
Pygame, a great module for creating games.

Pythoncard
Pythoncard is a module for making GUIs.

python eggs
Just a shit ton of links

Hello World!

This is my blog where I will be posting about anything I find interesting.
Hope you enjoy.