Monday, June 25, 2007

Mari belajar window batch programs

filename Batch files end with .BAT there is no magical string like in Un*x batch file commands are case insensitive
comments rem at line start is a comment line
in/output redirection cmd out-file-new
cmd >>out-file-append
stderr cannot be redirected, always goes to screen.
parameters %1 to %9 arguments number 1 to 9 given to the batch file %* all arguments given to the batch file (max. 9)
environment variables %varname%
conditions if [not] "string1" == "string2" command Enclose vars and strings in "". Idiom for empty check:
if "%var%" == "" command ("" needed for empty string).
if [not] exist filename command
if errorlevel 1 would test for any abnormal exit. errorlevel is the number the last program executed returned. The condition is true if the number is >= the errorlevel. Normal exit returns 0.
logical operators &&execute only when last prog returned ok
||execute only when last prog returned error
skript loops for %% var in ( group ) do command [params]
var wird zu jedem in group angegebenen dateinamen, eg for %%f in (*.*) do type %%f
skript jumps goto mark The target mark itself has to be prepended with : so NT can skip it during execution.
:mark
skript chaining call blah.bat calls another batch like a subroutine, i.e. the current batch's execution is resumed after the other one ends. Recursion possible.
skript messages echo [on|off][message]
@ at line start suppress showing of any single line
@echo off showing of any line (including this). Echo with message prints message.
getting user input for skripts pause[message] halts until any key pressed.
... shift shifts the params in a batch one down, e.g. %9 to %8. %0 is lost and if there were more than 10 params, the current number 11 becomes %9. Example loop with shift
        :next
if "%0" == "" goto end
type %0
shift
goto next
:end

curi dari http://www.schacherer.de/frank/tech/tools/batch.html