Sunday, June 20, 2010

My simple batch file tutorial [Part 6]

We all back to much more advance type of looping. :) Do you still remember the last batch sample programs of L4D installer (Part 5) ? Well , we are using another type of looping which will shows the progress bar of how complete the programs is installed. The sample  programs shows how it is done



@echo off
for /L %%d in (0, 10, 100) do echo Installing Left 4 Dead . . . . . %%d percent completed
pause


As you can see, we are using FOR command,
the FOR command is to be used for looping within a specific range .


If we see from the output, we can how the installing progress from 0% until 100% with just one lines compares to typing "Installing Left 4 Dead ..... ?? percent completes" 10 times.



We now go through the components in the wait command that make it ticks


for /L %%d in (0, 10, 100) do echo Installing Left 4 Dead . . . . . %%d percent completed

"for /L " is the switch command for loop that is used for looping across specific numbers
"%%d" is the variable that is used to temporary stores the numbers for the programs
Note: You can change the d into any letters you like ; "%%a",""%%b","%%c" is also acceptable.
"in (0,10,100)" is the range of numbers that you want to specify for the looping
number 0 (left) represents the starting range number in the loop,
number 10  (middle) represents the increment number in the loop
number 100 (right) represents the ending range number in the loop
"do" is the actions that you want to take after the loop number is calculated such as goto , echo , etc

Besides the positive increment, we can also do the negative increment that are shown belows.



Let's say that this is the combat logs for the Team Alpha of the Night Guard to eradicate zombies, we can see that there are doing pretty well until there are 5 zombies left before they request the emergency extraction.

@echo off
echo Night Guard: Activating combat warfare systems
echo Team Alpha Killing Logs
for /L %%z in (53595, -5359, 0) do echo Updating:%%z zombies left 
echo Night Guard: Emergency extraction requested 
pause 


It is almost the same just the increment values (middle number) we changed into negative values (-5359) also noted the number on the left is bigger than the number on the right.


Now let us go back to Left 4 Dead installer , let's go on the recap, the installations programs remind you how the Left 4K Dead always failed no matter what the conditions right ?


Now let us add the CD-Key authentications part to prevent any frustration from the legit buyers which always sports the "Buy Ori" messages.



@echo off
echo Please enter the CD serial key
:start
echo CD Serial Keys : 
set /p cd=
if %cd%==abcd-1234 (
echo Authentication Passed. Proceed to Installations
) else (
echo Authentication Failed. Please retry again.
goto start
)
pause


From what we can see above, we can learn two more new things that is IF Loop and the string comparisons. 


"IF Loop" is to used to presents the two different outcomes to the user if the conditions are met or not met.


Simplify the if loop statement into the everyday language (pseudo-code)

if set a conditions (
do this when conditions are met
)else (
do this instead when conditions are not met
)



String comparisons is to be used when to compare the input data to the set data for comparison purposes


Let us take the examples above :





set /p cd=
if %cd%==abcd-1234



"set /p" is the typical set parameters command
"cd" is the variable for storing the comparison string data.
(Note : you can change cd to any words that you like)
"==" is the equal to comparisons

Okay , that is for now. On the last part, I try to show you how to build  a GUI interface (albeit primitive) for the batch tutorial. 

No comments:

Another random post to read ? Come !

Related Posts with Thumbnails