Thursday, June 17, 2010

My simple batch file tutorial [Part 5]

Today , I shall go through a vital part in programming that is looping. In batch file programming, it is abeilt much simpler to understand than the other programming language. I provide you one of my sample programs. This sample is a bit long but hey, we all must get out from baby stages right?

@echo off
goto intro 
goto install
goto genuine
goto end
:intro
echo -----------------------------
echo Left 4K Dead Quick Installer
echo -----------------------------
echo Copyright Valve 1987
pause
cls
:install
echo Copying From Diskette Drive . 
@ping 127.0.0.1 -n 2 -w 1000 > nul
cls
echo Copying From Diskette Drive . .
@ping 127.0.0.1 -n 2 -w 1000 > nul
cls
echo Copying From Diskette Drive . . .
@ping 127.0.0.1 -n 2 -w 1000 > nul
cls
echo Copying From Diskette Drive . . . .
@ping 127.0.0.1 -n 2 -w 1000 > nul
cls
echo Copying From Diskette Drive . . . . .
@ping 127.0.0.1 -n 2 -w 1000 > nul
cls
echo Left 4K Dead is successfully installed
pause
cls
:genuine
echo Cyclic Redundancy Check
:end
echo You are using pirated version of Left 4K Dead !
echo  Buy original , support original 
pause

If you truly followed the tutorials part by part, you must pretty much know what does this programs does , right ? Actually, this is the alternate version of Left 4 Dead if it were to be made in 1987 where the era of 1MB RAM will cost more than 1 TB hard disk today... :P


Enough of small talks, you seen pretty much of new commands right 


We starts out from : ( Not that :: , refer to past tutorial) and goto 


: is the prefix variable to a sub module of the program
goto is the calling function command with respect to :


To make a sense , we simplify the concept just a bit. We analogized the : to the boxes and goto is the computer terminal controlling robotic pickup arm. Let say we have 3 boxes, ( A, B, C ). If we want box A , we input A to order the robotic arm to pickup box A and so on and so forth.


It is same case as the : and the goto, where as the : is the sub routine of the programs and the goto is the call functions for the program. For example, if we have a sub functions named :A that contains addition calculations, we can call the addition functions anytime by using "goto :A" command.


wait command is the command that holds all programs activity until timer expires


In batch file , there is NO "wait" command although it would make life easier. Hence, we can substitute it with redundant actions like pinging your own local IP address


@ping 127.0.0.1 -n 2 -w 1000 > nul


Don't fret if you see such a "long" command , I will explain it piece by piece


@ping is the ping command
127.0.0.1 is your own localhost LAN IP address. It's same for every computer
-n 2 is the number of ping it should tries
-w 1000 is our "wait command" but it isn't recognized as individually.The timer usually in milliseconds.
> nul is the we nullify the output broadcast not to let user know what we are up to .... :P 
(Note: There is another command "timeout delay 1000000" that was once used but nowadays the microprocessor works so fast until we cant see the broadcast messages, hence the network ping is still the best bet.)

If we didn't put the > nul, we are  going to see some interesting side effects.


(Damn you , Ubisoft ! You silently force Internet connection for Assassin Creed ? I better trust my old Uncle Pirate  ! )


That's it for now, for next part of tutorial, I am gonna show you the another looping type and the how to detect keystroke. :) See yah all ... 

No comments:

Another random post to read ? Come !

Related Posts with Thumbnails