BAT File Tips

Let’s play with the BAT files a little. As you may know, the BAT files contains commands for the Microsoft command processor, named cmd.exe. These set of commands forms a programming language that is very close to BASIC. The early versions of the BAT-files language (which comes from famous Windows ancestor, MS-DOS) were very limited. Nowadays it has more features and a few days ago I created a small collection of interesting tricks, related to BAT file programming. You can check the following examples that demonstrate which execution flow controlling statements BAT-file language has and show their analogs in PHP.

Simple IF statement that checks whether the VAR variable contains 1 and, if so, prints the message “VAR is 1”:

BAT-file:
SET VAR=1
IF "1" == "%VAR%" ECHO VAR is 1
PHP:
$var = 1;
if (1 == $var) echo 'VAR is 1';

Executing more then one command in IF true statement:

BAT-file:
IF EXIST MYFILE.TXT (
    DEL MYFILE.TXT
    ECHO File is deleted
) ELSE (
    ECHO File does not exist
)
PHP:
if (file_exist('MYFILE.TXT')) {
    unlink('MYFILE.TXT');
    echo 'File is deleted';
} else {
    echo 'File does not exist';
}

Using switch-like construction instead of series of IF statements (sorry, no default case is possible with BAT-file):

BAT-file:
GOTO CASE_%I%
:CASE_0
    ECHO i equals 0
    GOTO END_SWITCH
:CASE_1
    ECHO i equals 1
    GOTO END_SWITCH
:END_SWITCH
PHP:
switch ($i) {
case 0:
    echo "i equals 0";
    break;
case 1:
    echo "i equals 1";
    break;
}

Defining and calling a procedure:

BAT-file:
CALL :UNPACK_FILE MY_FILE.ZIP

REM The GOTO :EOF separates the main 
REM program and procedures.
GOTO :EOF

REM Procedure UNPACK_FILE
REM @param string %1
:UNPACK_FILE
    7z.exe x %1
GOTO :EOF
PHP:
unpackFile('MY_FILE.ZIP');

/**
 * unpackFile function
 * @param string $file
 */
function unpackFile($file) {
    exec("7z.exe x $file");
}

Calling a procedure by the name:

BAT-file:
SET PROCEDURE=UNPACK_FILE_7Z
CALL :%PROCEDURE% MY_FILE.ZIP
PHP:
$procedure = 'unpackFile7z';
$procedure('MY_FILE.ZIP');

Modify variable by its name:

BAT-file:
SET VARIABLE=VAR1
SET %VARIABLE%=TEST
ECHO %VAR1%
PHP:
$variable = 'var1';
$$variable = 'TEST'
echo $var1;

Save command result into variable (actually, it saves into the variable only the first line of the command result):

BAT-file:
DIR > RESULT.TXT
SET /P DIR_RESULT=< RESULT.TXT
DEL RESULT.TXT
PHP:
exec('DIR', $output);
$dirResult = $output[0];

String Replace:

BAT-file:
SET MSG=Hello username!
SET MSG=%MSG:username=Tom%
ECHO %MSG%
PHP:
$msg = 'Hello username!';
$msg = str_replace('username', 'Tom', $msg);
echo $msg;

String Spit:

BAT-file:
SET DATE=2007-08-23

CALL :SPLIT_DATE %DATE:-= %
ECHO YEAR: %YEAR%
ECHO MONTH: %MONTH%
ECHO DAY: %DAY%
GOTO :EOF

:SPLIT_DATE
SET YEAR=%1
SET MONTH=%2
SET DAY=%3
GOTO :EOF
PHP:
$date = '2007-08-23';

list($year, $month, $day) = implode('-', $date);

echo 'Year :' . $year;
echo 'Month: ' . $month;
echo 'Day: ' . $day;

Search for all text files with text TEMP and remove them:

for /f "delims=" %i in ('findstr /s /m zyx c:\*.bat') do @del "%i"

See also about BAT files and Windows Shell:

And remember - you can just install MinGW and get full power of linux command line and Bash.