www.xbdev.net
xbdev - software development
Monday December 30, 2024
Home | Contact | Support | Batch (.bat) Scripts... Learn to Love Windows Scripting ..
     
 

Batch (.bat) Scripts...

Learn to Love Windows Scripting ..

 








Batch .bat Scripting - Working Smarter Not Harder


you'll love batch scripting once you give it a try
You'll find yourself shouting about how much you love batch scripts - once you taste their power, their simplicity, their naughtness - they can be a time saver - but also fun! Who needs crosswords and wordsearches - when you have batch files!!



• Have fun with batch scripting
• Learn basics of batch script
• Develop helpful scripts (common scripts)
• Fun scripts (mini games/tools)
• Learn through practical examples


To kick things off, here's the "Hello, World!" script in batch:

@echo off
echo HelloWorld!
pause


The single most popular, minimal, and universally recognized batch script is undoubtedly the classic "Hello, World!" script.



Why Learn Batch Scripting??
Learning batch scripting opens up a world of powerful, practical, and incredibly fun possibilities right at your fingertips! With just a few lines of code, you gain control over your computer in ways that feel almost magical—automating tasks, creating custom tools, and solving complex problems with ease. Batch scripting empowers you to streamline repetitive tasks, organize your files instantly, and even interact with your system’s environment in ways that most users only dream of. And it doesn’t stop there! Batch scripting is an ideal gateway into programming logic, making it a valuable and entertaining skill that sparks creativity and sharpens problem-solving skills. Whether you're scripting simple file operations or creating your own mini-games for fun, the journey of mastering batch scripting is endlessly rewarding and downright exciting!


Interesting Scripts


Fun with Recursive Functions


Who said the batch language isn't very modular?! Did you know that you can write functions (even recursive ones)?


Click for details

We define a function in the bat file using a 'label' - then call it! Easy?

The example script below creates a function called
"add"
, is just a label and what comes after it in the same line are ignored
/
free text.

As a convention, we put the argument number and its description. The
"add"
function is defined to take a return variable name (argument
#1
) and two numbers
"a"
(argument
#2
) and
"b"
(argument
#3
).

The
"main"
function calls
"add"
using the
"call :func_name"
syntax and passing the arguments thereafter. When
"add"
returns, it may call
"goto :eof"
or
"exit /b <return_code>"
.

@echo off

setlocal

:main
    
echo Hello!
    
set /p a=Enter the first number:
    
set /p b=Enter the second number:

    
call :add result %a% %b%
    echo 
The result is: %result%
    goto :
eof

:add <1=return var> <2=first number> <3=second number>
    echo 
Adding '%2' and '%3'Returning the result into the variable '%1'
    
set /%1=%2+%3
    
goto :eof



Mixing Python and Batch files


A very fun trick is when we can have a script that runs successfully under two or more languages. Let's illustrate how to do a mixed script for running Python and Batch files.


Click for details

The below is a Batch script that invokes itself again as a Python script using the
"python -x"
switch (skip the first line).

Such scripts is useful when you first want a Batch script to set up or verify the right environment before invoking its embedded Python code.

@echo off
rem 
"""
:: From here and on, write any Batch file syntax and it will be ignored by Python

python -x "
%~f0" %*
exit /b %errorlevel%
:: End of batch file commands
"""

# Anything here is interpreted by Python

import platform
import sys

print("Hello world from Python %s!\n" platform.python_version())
print(
"The passed arguments are: %s" sys.argv[1:])



Mixing C++ and Batch files


Similar to the previous example with Python, we can make a Batch file script that is also a C++ program. Here's how we can make a self-compiling C++ Batch script.


Click for details

There's a slight drawback in this script below. The first line (
"/*"
a C multi-line comment opening) is not a valid command and it will cause an error - but we redirect the error to the
nul
device. We close the multi-line comment (with
"*/"
) shortly after exiting the Batch file.

/* 2>nul && echo | set /p=
@echo off
setlocal
echo.                                                                                                     
::
:: Self-compiling C++ Batch file!
::

set outfile="%~dpn0.exe"

cl %~dpf0 /TP /EHsc /link /out:%outfile% /nologo > nul

if exist %outfile% (
    %outfile%
    del %outfile%
    del "%~dpn0.obj"
) else (
  echo Compilation failed!
)
goto :eof
*/

#include <stdio.h>

int main()
{
    
printf(
        
"Hello world! I was self-compiled!\n"
        "\n"
        "Checkout the Batchography book!\n"
);
    
    return 
0;
}



Batch File Games


Rubrik Cube Game (.bat)



Click for details

@echo off
mode con
lines=32
setlocal enabledelayedexpansion

set up_x_1
0  1  2  3  5  6  7  8  9 10 11 18 19 20 27 28 29 36 37 38
set up_x_2
2  5  8  1  7  0  3  6 36 37 38  9 10 11 18 19 20 27 28 29

set md_x_1
=12 13 14 21 22 23 30 31 32 39 40 41
set md_x_2
=39 40 41 12 13 14 21 22 23 30 31 32

set dw_x_1
=15 16 17 24 25 26 33 34 35 42 43 44 45 46 47 48 50 51 52 53
set dw_x_2
=42 43 44 15 16 17 24 25 26 33 34 35 51 48 45 52 46 53 50 47

set le_y_1
0  3  6  9 12 15 29 32 35 36 37 38 39 41 42 43 44 51 52 53
set le_y_2
9 12 15 51 52 53  6  3  0 38 41 44 37 43 36 39 42 35 32 29

set md_y_1
1  4  7 10 13 16 28 31 34 48 49 50
set md_y_2
=10 13 16 48 49 50  7  4  1 34 31 28

set ri_y_1
2  5  8 11 14 17 18 19 20 21 23 24 25 26 27 30 33 45 46 47
set ri_y_2
=11 14 17 45 46 47 24 21 18 25 19 26 23 20  8  5  2 33 30 27

set le_z_1
6  7  8  9 10 11 12 14 15 16 17 18 21 24 38 41 44 45 48 51
set le_z_2
=18 21 24 11 14 17 10 16  9 12 15 45 48 51  8  7  6 44 41 38

set md_z_1
3  4  5 19 22 25 37 40 43 46 49 52
set md_z_2
=19 22 25 46 49 52  5  4  3 43 40 37

set ri_z_1
0  1  2 20 23 26 27 28 29 30 32 33 34 35 36 39 42 47 50 53
set ri_z_2
=20 23 26 47 50 53 33 30 27 34 28 35 32 29  2  1  0 42 39 36

set keys_moves
=zaxsbgnhdfcv
set keys_rotate
=kilom,

set keys[%keys_moves:~0,1%]="%le_y_1%" "%le_y_2%"
set keys[%keys_moves:~1,1%]="%le_y_2%" "%le_y_1%"
set keys[%keys_moves:~2,1%]="%ri_y_1%" "%ri_y_2%"
set keys[%keys_moves:~3,1%]="%ri_y_2%" "%ri_y_1%"
set keys[%keys_moves:~4,1%]="%le_z_1%" "%le_z_2%"
set keys[%keys_moves:~5,1%]="%le_z_2%" "%le_z_1%"
set keys[%keys_moves:~6,1%]="%ri_z_1%" "%ri_z_2%"
set keys[%keys_moves:~7,1%]="%ri_z_2%" "%ri_z_1%"
set keys[%keys_moves:~8,1%]="%up_x_1%" "%up_x_2%"
set keys[%keys_moves:~9,1%]="%up_x_2%" "%up_x_1%"
set keys[%keys_moves:~10,1%]="%dw_x_1%" "%dw_x_2%"
set keys[%keys_moves:~11,1%]="%dw_x_2%" "%dw_x_1%"
set keys[%keys_rotate:~0,1%]="%le_y_1% %md_y_1% %ri_y_1%" "%le_y_2% %md_y_2% %ri_y_2%"
set keys[%keys_rotate:~1,1%]="%le_y_2% %md_y_2% %ri_y_2%" "%le_y_1% %md_y_1% %ri_y_1%"
set keys[%keys_rotate:~2,1%]="%le_z_1% %md_z_1% %ri_z_1%" "%le_z_2% %md_z_2% %ri_z_2%"
set keys[%keys_rotate:~3,1%]="%le_z_2% %md_z_2% %ri_z_2%" "%le_z_1% %md_z_1% %ri_z_1%"
set keys[%keys_rotate:~4,1%]="%up_x_1% %md_x_1% %dw_x_1%" "%up_x_2% %md_x_2% %dw_x_2%"
set keys[%keys_rotate:~5,1%]="%up_x_2% %md_x_2% %dw_x_2%" "%up_x_1% %md_x_1% %dw_x_1%"

set colors[0]= 
set colors[1]=#
set colors[2]=O
set colors
[3]=%%
set colors
[4]=@
set colors
[5]=.

for /%%f in (015) do (
    for /
%%x in (0,1,8) do (
        
set /a res = %%f*9+%%x
        set a
[!res!]=!colors[%%f]!
    )
)



:
start
cls
echo   ____  _   _ ____ ___ _  ___
echo  ^|  \^| ^| ^| ^| __ ^)_ _^| ^|/ ^( ^)___
echo  ^| ^|_^) ^| ^| ^| ^|  _ \^| ^|^| ' /^|// __^|
echo  ^|  _ ^<^| ^|_^| ^| ^|_^) ^| ^|^| . \  \__ \
echo  ^|_^| \_\\___/^|____/___^|_^|\_\ ^|___/
echo     / ___^| ^| ^| ^| __ ^)^| ____^|
echo    ^| ^|   ^| ^| ^| ^|  _ \^|  _^|
echo    ^| ^|___^| ^|_^| ^| ^|_^) ^| ^|___
echo     \____^|\___/^|____/^|_____^|
echo             by GiAnMMV
pause

cls
echo Scrambling...
for /l %%f in (0, 1, 100) do (
    set /a res=!random! %% 12
    call call call:rotate %%%%keys[%%keys_moves:~!res!,1%%]%%%%
    <nul set /p=^|
)
set count=0
set proj=0

:loop
cls
if %proj%==0 (call:isometric) else (call:cabinet)

if defined com set com=!com:~1!
if not defined com (set /p "com=Enter command: ")
if not defined com goto loop

if /i !com!==exit cls & goto exit
if /i !com!==proj set /a "proj=(!proj!+1)%%2" & set com= & goto loop

call:rotate !keys[%com:~0,1%]!

if not "%keys_moves%"=="!keys_moves:%com:~0,1%=!" (
    set /a count+=1
    for /l %%f in (0, 9, 45) do (
        set /a res=%%f+1
        set /a res2=%%f+8
        for /l %%n in (!res!, 1, !res2!) do (
            if not !a[%%f]!==!a[%%n]! goto loop
        )
    )
    goto end
)
goto loop

:end
cls
echo You won^^! Congratulations^^!
echo You solved the cube in %count% moves.
pause
endlocal
exit /b

:exit
set /p com=Are you sure you want to exit? (Y/N) 
if /i %com%==y (
    endlocal
    exit /b
)
if /i %com%==n (
    set com=
    goto loop
)
echo Invalid selection.
goto exit

:isometric
echo          /\
echo         /%a[0]%%a[0]%\
echo        /%a[0]%%a[0]%%a[0]%%a[0]%\
echo       /\%a[0]%%a[0]%%a[0]%%a[0]%/\
echo      /%a[3]%%a[3]%\%a[0]%%a[0]%/%a[1]%%a[1]%\
echo     /%a[3]%%a[3]%%a[3]%%a[3]%\/%a[1]%%a[1]%%a[1]%%a[1]%\        Moves: %count%
echo    /\%a[3]%%a[3]%%a[3]%%a[3]%/\%a[1]%%a[1]%%a[1]%%a[1]%/\       +---------------------+
echo   /%a[6]%%a[6]%\%a[3]%%a[3]%/%a[4]%%a[4]%\%a[1]%%a[1]%/%a[2]%%a[2]%\      ^|      CONTROLS:      ^|
echo  /%a[6]%%a[6]%%a[6]%%a[6]%\/%a[4]%%a[4]%%a[4]%%a[4]%\/%a[2]%%a[2]%%a[2]%%a[2]%\     +----------+----------+
echo ^|\%a[6]%%a[6]%%a[6]%%a[6]%/\%a[4]%%a[4]%%a[4]%%a[4]%/\%a[2]%%a[2]%%a[2]%%a[2]%/^|    ^|MOVES     ^|ROTATIONS ^|
echo ^|\\%a[6]%%a[6]%/%a[7]%%a[7]%\%a[4]%%a[4]%/%a[5]%%a[5]%\%a[2]%%a[2]%//^|    ^|  U   d   ^|  X   i   ^|
echo ^|%a[9]%\\/%a[7]%%a[7]%%a[7]%%a[7]%\/%a[5]%%a[5]%%a[5]%%a[5]%\//%a[20]%^|    ^|  U'  
f   ^|  X'  k   ^|
echo ^|%a[9]%%a[9]%\\%a[7]%%a[7]%%a[7]%%a[7]%/\%a[5]%%a[5]%%a[5]%%a[5]%//%a[20]%%a[20]%^|    ^|  D   v   ^|  Y   m   ^|
echo ^|%a[9]%%a[9]%^|\\%a[7]%%a[7]%/%a[8]%%a[8]%\%a[5]%%a[5]%//^|%a[20]%%a[20]%^|    ^|  D'  
c   ^|  Y'  ,   ^|
echo ^|\%a[9]%^|%a[10]%\\/%a[8]%%a[8]%%a[8]%%a[8]%\//%a[19]%^|%a[20]%/^|    ^|  R   s   ^|  Z   l   ^|
echo ^|%a[12]%\^|%a[10]%%a[10]%\\%a[8]%%a[8]%%a[8]%%a[8]%//%a[19]%%a[19]%^|/%a[23]%^|    ^|  R'  
x   ^|  Z'  o   ^|
echo ^|%a[12]%%a[12]%\%a[10]%%a[10]%^|\\%a[8]%%a[8]%//^|%a[19]%%a[19]%/%a[23]%%a[23]%^|    ^|  L   z   +----------+
echo ^|%a[12]%%a[12]%^|\%a[10]%^|%a[11]%\\//%a[18]%^|%a[19]%/^|%a[23]%%a[23]%^|    ^|  L'  
a   ^|CHANGE    ^|
echo ^|\%
a[12]%^|%a[13]%\^|%a[11]%%a[11]%\/%a[18]%%a[18]%^|/%a[22]%^|%a[23]%/^|    ^|  F   b   ^|PROJECTION^|
echo ^|%
a[15]%\^|%a[13]%%a[13]%\%a[11]%%a[11]%^|^|%a[18]%%a[18]%/%a[22]%%a[22]%^|/%a[26]%^|    ^|  F'  g   ^|      proj^|
echo ^|%a[15]%%a[15]%\%a[13]%%a[13]%^|\%a[11]%^|^|%a[18]%/^|%a[22]%%a[22]%/%a[26]%%a[26]%^|    ^|  B   h   +----------+
echo ^|%a[15]%%a[15]%^|\%a[13]%^|%a[14]%\^|^|/%a[21]%^|%a[22]%/^|%a[26]%%a[26]%^|    ^|  B'  
n   ^|EXIT  exit^|
echo  \%
a[15]%^|%a[16]%\^|%a[14]%%a[14]%^|^|%a[21]%%a[21]%^|/%a[25]%^|%a[26]%/     +----------+----------+
echo   \^|%
a[16]%%a[16]%\%a[14]%%a[14]%^|^|%a[21]%%a[21]%/%a[25]%%a[25]%^|/
echo    \%
a[16]%%a[16]%^|\%a[14]%^|^|%a[21]%/^|%a[25]%%a[25]%/
echo     \%
a[16]%^|%a[17]%\^|^|/%a[24]%^|%a[25]%/
echo      \^|%
a[17]%%a[17]%^|^|%a[24]%%a[24]%^|/
echo       \%
a[17]%%a[17]%^|^|%a[24]%%a[24]%/
echo        \%
a[17]%^|^|%a[24]%/
echo         \^|^|/
echo          \/
exit /
b 0

:cabinet
echo                         Moves: %count%
echo       ---------------
// +---------------------+
echo      /%a[0]%%a[0]%%a[0]%%a[0]%/%a[1]%%a[1]%%a[1]%%a[1]%/%a[2]%%a[2]%%a[2]%%a[2]%//^| ^|      CONTROLS:      ^|
echo     /----/----/----//%a[20]%^| +----------+----------+
echo    /%a[3]%%a[3]%%a[3]%%a[3]%/%a[4]%%a[4]%%a[4]%%a[4]%/%a[5]%%a[5]%%a[5]%%a[5]%//^|%a[20]%^| ^|MOVES     ^|ROTATIONS ^|
echo   /----/----/----//%a[19]%^|/^| ^|  U   d   ^|  X   i   ^|
echo  /%a[6]%%a[6]%%a[6]%%a[6]%/%a[7]%%a[7]%%a[7]%%a[7]%/%a[8]%%a[8]%%a[8]%%a[8]%//^|%a[19]%^|%a[23]%^| ^|  U'  f   ^|  X'  k   ^|
echo /----/----/----//%a[18]%^|/^|%a[23]%^| ^|  D   v   ^|  Y   m   ^|
echo ^|%a[9]%%a[9]%%a[9]%%a[9]%^|%a[10]%%a[10]%%a[10]%%a[10]%^|%a[11]%%a[11]%%a[11]%%a[11]%^|^|%a[18]%^|%a[22]%^|/^| ^|  D'  c   ^|  Y'  ,   ^|
echo ^|%
a[9]%%a[9]%%a[9]%%a[9]%^|%a[10]%%a[10]%%a[10]%%a[10]%^|%a[11]%%a[11]%%a[11]%%a[11]%^|^|/^|%a[22]%^|%a[26]%^| ^|  R   s   ^|  Z   l   ^|
echo ^|----+----+----^|^|%
a[21]%^|/^|%a[26]%^| ^|  R'  x   ^|  Z'  o   ^|
echo ^|%
a[12]%%a[12]%%a[12]%%a[12]%^|%a[13]%%a[13]%%a[13]%%a[13]%^|%a[14]%%a[14]%%a[14]%%a[14]%^|^|%a[21]%^|%a[25]%^|/  ^|  L   z   +----------+
echo ^|%
a[12]%%a[12]%%a[12]%%a[12]%^|%a[13]%%a[13]%%a[13]%%a[13]%^|%a[14]%%a[14]%%a[14]%%a[14]%^|^|/^|%a[25]%/   ^|  L'  a   ^|CHANGE    ^|
echo ^|----+----+----^|^|%a[24]%^|/    ^|  F   b   ^|PROJECTION^|
echo ^|%a[15]%%a[15]%%a[15]%%a[15]%^|%a[16]%%a[16]%%a[16]%%a[16]%^|%a[17]%%a[17]%%a[17]%%a[17]%^|^|%a[24]%/     ^|  F'  
g   ^|      proj^|
echo ^|%
a[15]%%a[15]%%a[15]%%a[15]%^|%a[16]%%a[16]%%a[16]%%a[16]%^|%a[17]%%a[17]%%a[17]%%a[17]%^|^|/      ^|  B   h   +----------+
echo +--------------^|/       ^|  
B'  n   ^|EXIT  exit^|
echo                         +----------+----------+
exit /b 0

:rotate
set r1=%~1
set r2=%~2

set x=0
for %%r in (%r1%) do (
    set r1[!x!]=%%r
    set l[!x!]=!a[%%r]!
    set /a x=!x!+1
)
set x=0
for %%r in (%r2%) do (
    set r2[!x!]=%%r
    set /a x=!x!+1
)
for /l %%r in (0, 1, %x%) do (
    set a[!r2[%%r]!]=!l[%%r]!
    set l[%%r]=
    set r1[%%r]=
    set r2[%%r]=
)
exit /b 0



Pacman Game (.bat Script)



Click for details
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

IF "%1" == "" (
    >
NUL REG ADD HKCU\Console\PacMan /v FaceName /t REG_SZ /"Lucida Console" /f
    START 
"PacMan" "%~0" Font
    
EXIT
) else IF 
"%1" == "Font" (
    >
NUL REG DELETE HKCU\Console\PacMan /f
) else (
    GOTO :%
1
)

TITLE PacMan
MODE 30
30
(CHCP 65001)>NUL

CALL 
:MACROS

SET 
"direction=W A S D"
SET "mov[W]=y]-=1"
SET "mov[A]=x]-=1"
SET "mov[S]=y]+=1"
SET "mov[D]=x]+=1"
SET "ghost[blinky]=%col:c=255;0;0%▓"
SET "ghost[inky]=%col:c=0;255;255%▓"
SET "ghost[pinky]=%col:c=255;184;255%▓"
SET "ghost[clyde]=%col:c=255;184;82%▓"
SET "ghost[base]= inky  blinky  pinky  clyde "
SET "ghost[cur]=%ghost[base]%"
SET "map[pelbou]= "
SET "map[powerup]=.6$7..6$23..22$7..22$23."
SET map[classic]="1-19"."1-1" "19-19"."1-1" "3-4" "6-8" "12-14" "16-17" "19-19"."1-1" "19-19"."1-1" "3-4" "6-8" "12-14" "16-17" "19-19"."1-1" "19-19"."1-4" "6-8" "12-14" "16-19"."4-4" "6-6" "14-14" "16-16"."1-4" "6-6" "8-8" "12-12" "14-14" "16-19"."1-1" "8-8" "12-12" "19-19"."1-4" "6-6" "8-8" "12-12" "14-14" "16-19"."4-4" "6-6" "14-14" "16-16"."1-4" "6-8" "12-14" "16-19"."1-1" "19-19"."1-1" "3-4" "6-8" "12-14" "16-17" "19-19"."1-1" "19-19"."1-1" "3-4" "6-8" "12-14" "16-17" "19-19""1-19"."1-1" "19-19"."1-19"
SET /"char[x]=d[x]=15","char[y]=d[y]=6""pac[invinc]=0""score=0"
SET /"inky[x]=inky[st]=14""inky[y]=14""clyde[x]=clyde[st]=15""clyde[y]=14""blinky[x]=blinky[st]=15""blinky[y]=12""pinky[x]=pinky[st]=16""pinky[y]=14"

CALL :CREATEMAP classic powerup

ECHO %ESC%[?25l%col:c=229;235;52%%ESC%[8;11HDIFFICULTY%ESC%[10;11H[AEASY%ESC%[12;11H[BNORMAL%ESC%[14;11H[CHARD
(CHOICE /C ABC /N)>NUL
IF %errorlevelEQU 1 (
    
SET "speed=55"
) else IF %errorlevelEQU 2 (
    
SET "speed=45"
) else (
    
SET "speed=35"
)

:
MENU
SETLOCAL
DEL 
"%~dpn0.quit" 2>NUL
"%~F0" CONTROL W >"%temp%\%~n0_signal.txt" "%~F0" GAME <"%temp%\%~n0_signal.txt"
ECHO %ESC%[25;6H[ATO REPLAY%ESC%[26;6H[BTO EXIT
(
CHOICE /C AB /N)>NUL
IF %errorlevelEQU 1 (
    
ENDLOCAL
    
GOTO :MENU
)
EXIT

:
GAME
ECHO %ESC%[2J%ESC%[5;6H%pel[disp]%%ESC%[5;5H%map[disp]%%ESC%[%char[y]%;%char[x]%H%col:c=229;235;52%
FOR /%%Q in (3, -10) DO (
    IF %%
Q EQU 0 (
         ECHO %
ESC%[24;13HSTART
    
) else (
         ECHO %
ESC%[24;15H%%Q
    
)
    FOR /
%%J in (131000000) DO REM
)
FOR /
%%# in () DO (
    
SET /"input="
    
IF defined input (
        
2>NUL SET /!every:#=20! || (
            
FOR %%A in (!input!) DO (
                
SET /"d[x]=char[x]""d[y]=char[y]""d[!mov[%%A]!"
                
FOR %%Q in (".!d[y]!$!d[x]!.") DO (
                    IF 
"%map[bou]%" == "!map[bou]:%%~Q=!" (
                        IF 
not "!map[powerbou]:%%~Q=!" == "!map[powerbou]!" (
                            
SET /"pac[invinc]=4""score+=50"
                            
SET "map[powerbou]=!map[powerbou]:%%~Q=!"
                        
) else IF "!map[pelbou]:%%~Q=!" == "!map[pelbou]!" (
                            
SET /"score+=10"
                            
SET "map[pelbou]=!map[pelbou]!%%~Q"
                        
)
                        ECHO %
ESC%[!char[y]!;!char[x]!
                        SET 
/"char[!mov[%%A]!"
                    
)
                )
            )
        )
    )
    
2>NUL SET /!every:#=%speed%! || (
        
SET "ghost[disp]="
        
IF !pac[invinc]! GTR 2 (
            
SET "ghost[add]=%col:c=255;255;255%▒"
        
) else IF !pac[invinc]! GTR 0 (
            
SET "ghost[add]=%col:c=10;18;148%▒"
        
) else (
            
SET "ghost[add]=#"
        
)
        FOR %%
G in (!ghost[cur]!) DO (
            
SET "ghost[dir]=N"
            
SET /"d[x]=%%G[x] - char[x]""d[y]=%%G[y] - char[y]"
            
IF !pac[invinc]! GTR 0 (
                IF !
d[y]! LSS 0 (
                    
SET "ghost[dir]=W"
                
) else IF !d[y]! GTR 0 (
                    
SET "ghost[dir]=S"
                
)
                IF 
"!ghost[dir]!" == "N" (
                    IF !
d[x]! LSS 0 (
                        
SET "ghost[dir]=A"
                    
) else IF !d[x]! GTR 0 (
                        
SET "ghost[dir]=D"
                    
)
                )
            ) else (
                IF !
d[y]! LSS 0 (
                    
SET "ghost[dir]=S"
                
) else IF !d[y]! GTR 0 (
                    
SET "ghost[dir]=W"
                
)
                IF 
"!ghost[dir]!" == "N" (
                    IF !
d[x]! LSS 0 (
                        
SET "ghost[dir]=D"
                    
) else IF !d[x]! GTR 0 (
                        
SET "ghost[dir]=A"
                    
)
                )
            )
            FOR %%
A in (!ghost[dir]!) DO (
                
SET /"d[x]=%%G[x]""d[y]=%%G[y]""d[!mov[%%A]!"
                
FOR /"tokens=1-2" %%Q in (".!%%G[y]!$!%%G[x]!. .!d[y]!$!d[x]!.") DO (
                    IF 
"%map[bou]%" == "!map[bou]:%%~R=!" (
                        IF 
not "!map[powerbou]:%%~Q=!" == "!map[powerbou]!" (
                            
SET "ghost[disp]=!ghost[disp]!%col:c=222;161;133%%ESC%[!%%G[y]!;!%%G[x]!H○"
                        
) else IF "!map[pelbou]:%%~Q=!" == "!map[pelbou]!" (
                            
SET "ghost[disp]=!ghost[disp]!%col:c=222;161;133%%ESC%[!%%G[y]!;!%%G[x]!H•"
                        
) else (
                            
SET "ghost[disp]=!ghost[disp]!%ESC%[!%%G[y]!;!%%G[x]!H "
                        
)
                        
SET /"%%G[!mov[%%A]!"
                    
) else (
                        
SET "dir[count]=0"
                        
SET "dir[pos]="
                        
FOR %%D in (!direction:%%A^=!) DO (
                            
SET /"d[x]=%%G[x]""d[y]=%%G[y]""d[!mov[%%D]!"
                            
FOR %%N in (".!d[y]!$!d[x]!.") DO (
                                IF 
"%map[bou]%" == "!map[bou]:%%~N=!" (
                                    
SET /"dir[count]+=1"
                                    
SET "dir[!dir[count]!]=%%D"
                                
)
                            )
                        )
                        
SET /"dir[rand]=(!RANDOM! %% dir[count]) + 1"
                        
FOR %%K in (!dir[rand]!) DO (
                            FOR %%
P in (!dir[%%K]!) DO (
                                IF 
not "!map[powerbou]:%%~Q=!" == "!map[powerbou]!" (
                                    
SET "ghost[disp]=!ghost[disp]!%col:c=222;161;133%%ESC%[!%%G[y]!;!%%G[x]!H○"
                                
) else IF "!map[pelbou]:%%~Q=!" == "!map[pelbou]!" (
                                    
SET "ghost[disp]=!ghost[disp]!%col:c=222;161;133%%ESC%[!%%G[y]!;!%%G[x]!H•"
                                
) else (
                                    
SET "ghost[disp]=!ghost[disp]!%ESC%[!%%G[y]!;!%%G[x]!H "
                                
)
                                
SET /"%%G[!mov[%%P]!"
                            
)
                        )
                    )
                )
            )
            IF 
"!ghost[add]!" == "#" (
                
SET "ghost[disp]=!ghost[disp]!%ESC%[!%%G[y]!;!%%G[x]!H!ghost[%%G]!"
                
IF "!%%G[y]!$!%%G[x]!" == "!char[y]!$!char[x]!" (
                    ECHO %
ESC%[24;6HGAME OVERPRESS [W]
                    (
COPY NUL "%~dpn0.quit")>NUL
                    
EXIT
                )
            ) else (
                IF 
"!%%G[y]!$!%%G[x]!" == "!char[y]!$!char[x]!" (
                    
SET /"%%G[y]=14""%%G[x]=%%G[st]""score+=200"
                    
SET "ghost[cur]=!ghost[cur]: %%G =!"
                
) else (
                    
SET "ghost[disp]=!ghost[disp]!%ESC%[!%%G[y]!;!%%G[x]!H!ghost[add]!"
                
)
            )
        )
    )
    
2>NUL SET /!every:#=500! || (
        
IF !pac[invinc]! GTR 0 (
            
SET /"pac[invinc]-=1"
            
IF !pac[invinc]! EQU 0 (
                
SET "ghost[cur]=!ghost[base]!"
            
)
        )
    )
    
2>NUL SET /!every:#=2! && (
        
SET "pac[col]=%col:c=229;235;52%"
    
) || (
        
SET "pac[col]=%col:c=194;157;12%"
    
)
    ECHO %
ESC%[4;6H%col:c=255;255;255%!score!!pac[col]!%ESC%[!char[y]!;!char[x]!H█!ghost[disp]!
    
SET /"frame+=1"
)

:
CREATEMAP <map> <powerup>
SETLOCAL
SET 
"map[data]=!map[%~1]!" SET "map[count]=1"
SET map[!map[count]!]=%map[data]:.= & SET /A map[count]+=SET map[!map[count]!]=%
FOR /
%%G in (1119) DO (
    
SET "pel[line]=!pel[line]!•"
)
FOR /
%%# in (1, 1, %map[count]%) DO (
    
SET "pel[disp]=!pel[disp]!%pel[line]%%dn:n=1%%bk:n=19%"
    
FOR %%Q in (!map[%%#]!) DO (
        
FOR /"tokens=1-2 delims=-" %%A in ("%%~Q") DO (
            
SET "map[disp]=!map[disp]!%ESC%[5G%ESC%[%%AC"
            
FOR /%%P in (%%A1, %%B) DO (
                
SET /"d[y]=%%# + 4","d[x]=%%P + 5"
                
SET "map[bou]=!map[bou]!.!d[y]!$!d[x]!"
                
SET "map[disp]=!map[disp]!█"
            
)
        )
    )
    
SET "map[disp]=!map[disp]!%dn:n=1%%ESC%[5G"
)
FOR %%
Q in (!map[%~2]:.^= !) DO (
    FOR /
"tokens=1-2 delims=$" %%A in ("%%Q") DO (
        
SET "pel[disp]=!pel[disp]!%ESC%[%%A;%%BH○"
    
)
)
ENDLOCAL SET "map[disp]=%col:c=33;33;222%%map[disp]%" SET "map[bou]=%map[bou]%." SET "map[powerbou]=!map[%~2]!" SET "pel[disp]=%col:c=222;161;133%%pel[disp]%"
GOTO :EOF

:CONTROL
FOR /%%C in () do (
    FOR /
"tokens=*" %%A in ('CHOICE /C:WASD /N') DO (
        IF 
exist "%~dpn0.quit" (
            
DEL "%~dpn0.quit"
            
EXIT
        )
        <
NUL SET /".=%%A"
    
)
)
GOTO :
EOF

:MACROS
FOR /%%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"
SET every="1/((frame %% #)^0)"
SET "col=%ESC%[38;2;cm"
SET "up=%ESC%[nA"
SET "dn=%ESC%[nB"
SET "bk=%ESC%[nD"
SET "nx=%ESC%[nC"
GOTO :EOF



Snake Batch (snake.bat Game)



Click for details
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
IF not "%1" == "" (
    GOTO :%
1
)
TITLE Snake
MODE 30
22
FOR /%%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"
(CHCP 65001)>NUL

SET 
"mov[W]=y]-=1"
SET "mov[A]=x]-=1"
SET "mov[S]=y]+=1"
SET "mov[D]=x]+=1"
SET "snake[bou]=.0;0."
SET "skin[1]=%ESC%[38;2;105;78;148m▒$%ESC%[38;2;184;20;184m▓"
SET "skin[2]=%ESC%[38;2;235;180;52m♣$%ESC%[38;2;209;109;38m♥"
SET "skin[3]=%ESC%[38;2;212;38;61m◙$%ESC%[38;2;222;100;116m◘"
SET /"map[width]=30""map[height]=15""char[0][x]=d[x]=3","char[0][y]=d[y]=3","snake[mass]=1","pel[x]=0","snake[speed]=16"

CALL :MAKEMAP %map[width]% %map[height]%

SET "exit=ECHO %ESC%[1;1H%disp[kill]%%ESC%[38;2;255;255;255m%ESC%[%map[height]%;1H%ESC%[3BOuch...Press W to Continue&(COPY NUL "%~dpn0.quit")>NUL&EXIT"
DEL "%~dpn0.quit" 2>NUL
ECHO %ESC%[?25l%ESC%[38;2;255;255;255mChoose your skin&FOR /%%Q in (113) DO (
    ECHO %
ESC%[B%ESC%[38;2;255;255;255m[%%Q] !skin[%%Q]:$=!!skin[%%Q]:$=!!skin[%%Q]:$=!
)
(
CHOICE /C 123 /N)>NUL
FOR /"tokens=1-2 delims=$" %%A in ("!skin[%errorlevel%]!") DO (
    
SET "snake[cha][1]=%%A"&SET "snake[cha][2]=%%B"
)
SET "snake[disp]=%ESC%[%char[0][y]%;%char[0][x]%H%ESC%[38;2;184;20;184m%snake[cha][2]%"

:START
SETLOCAL
"%~F0" CONTROL >"%temp%\%~n0_signal.txt" "%~F0" GAME <"%temp%\%~n0_signal.txt"
ENDLOCAL
GOTO :START

:GAME
FOR /%%# in () DO (
    
SET /"input="
    
SET /"frames+=1"
    
IF !pel[x]! EQU 0 (
        
SET /"pel[x]=!RANDOM! * ((%map[width]% - 1) - 2 + 1) / 32768 + 2""pel[y]=!RANDOM! * ((%map[height]% - 1) - 2 + 1) / 32768 + 2"
        
SET "pel[disp]=%ESC%[!pel[y]!;!pel[x]!H%ESC%[38;2;50;168;82m■"
    
)
    IF 
defined input (
        FOR %%
A in (!input!) DO (
            IF 
not "!input[cur]!" == "!mov[%%A]!" (
                
SET "input[new]=1"
            
)
            
SET "input[cur]=!mov[%%A]!"
        
)
    )
    (
SET /"1/((((~(0-(frames %% snake[speed]))>>31)&1)&((~((frames %% snake[speed])-0)>>31)&1))|input[new])" 2>NUL) && (
        
SET /"d[x]=char[0][x]","d[y]=char[0][y]","d[!input[cur]!","input[new]=0"
        
IF !d[x]! LSS %map[width]% ( IF !d[x]! GTR 1 (
            IF !
d[y]! LSS %map[height]% ( IF !d[y]! GTR 1 (
                
SET "snake[disp]="
                
SET /"d[shift][x]=char[0][x]=d[x]","d[shift][y]=char[0][y]=d[y]"
                
IF "!pel[x]!;!pel[y]!" == "!d[x]!;!d[y]!" (
                    
SET /"pel[x]=0","snake[mass]+=1"
                
)
                IF !
snake[mass]! NEQ 1 (
                    FOR %%
Q in (".!d[x]!;!d[y]!.") DO (
                        IF 
not "!snake[bou]!" == "!snake[bou]:%%~Q=!" (
                            %exit%
                        )
                    )
                )
                
SET "snake[bou]="
                
FOR /%%M in (11, !snake[mass]!) DO (
                    
SET "snake[bou]=!snake[bou]!.!char[%%M][x]!;!char[%%M][y]!"
                    
SET /"d[save][x]=char[%%M][x]","d[save][y]=char[%%M][y]""char[%%M][x]=d[shift][x]","char[%%M][y]=d[shift][y]""d[shift][x]=d[save][x]","d[shift][y]=d[save][y]","d[disp]=%%M %% 2"
                    
IF !d[disp]! EQU 0 (
                        
SET "snake[disp]=!snake[disp]!%ESC%[!char[%%M][y]!;!char[%%M][x]!H%ESC%[38;2;105;78;148m%snake[cha][1]%"
                    
) else (
                        
SET "snake[disp]=!snake[disp]!%ESC%[!char[%%M][y]!;!char[%%M][x]!H%ESC%[38;2;184;20;184m%snake[cha][2]%"
                    
)
                )
                
SET "snake[bou]=!snake[bou]!."
            
) else (%exit%)) else (%exit%)
        ) else (%exit%)) else (%exit%)
    )
    ECHO %
ESC%[2J%ESC%[1;1H%disp[map]%!pel[disp]!!snake[disp]!%ESC%[38;2;255;255;255m%ESC%[%map[height]%;1H%ESC%[BScore : !snake[mass]!%ESC%[1G%ESC%[BUse WASD to move
)

:
MAKEMAP <width> <height>
SETLOCAL
SET 
"disp[border]="
FOR /%%W in (11, %1) DO (
    
SET "disp[border]=!disp[border]!█"
)
SET /A  "width[adj]=%1 - 2","height[adj]=%2 - 1"
SET "disp[map]=%disp[border]%#%ESC%[B%ESC%[%1D%disp[border]%"
FOR /%%H in (21, %height[adj]%) DO (
    
SET "disp[map]=!disp[map]:#=%ESC%[B%ESC%[%1D█%ESC%[%width[adj]%C█#!"
)
SET "disp[map]=%disp[map]:#=%"
ENDLOCAL&SET "disp[map]=%ESC%[38;2;148;194;224m%disp[map]%"&SET "disp[kill]=%ESC%[38;2;41;100;138m%disp[map]:█=X%"
GOTO :EOF

:CONTROL
FOR /%%C in () do (
    FOR /
"tokens=*" %%A in ('CHOICE /C:WASD /N') DO (
        IF 
exist "%~dpn0.quit" (
            
DEL "%~dpn0.quit"
            
EXIT
        )
        <
NUL SET /".=%%A"
    
)
)
GOTO :
EOF




Quick Cheatsheet


Table listing common batch (.bat) script commands, functions, and related information, including descriptions and examples:

Command/Function Description Example
@echo Toggles command echoing on/off in the script. @echo off
echo Displays text or a message in the command prompt. echo Hello, World!
REM Adds a comment to the script. REM This is a comment.
:: Alternative way to add comments. :: Another comment style.
set Sets or displays environment variables. set VAR=value
%VAR% Accesses the value of an environment variable. echo %VAR%
if Performs conditional processing in the script. if "%VAR%" == "value" echo Match
goto Jumps to a labeled section of the script. goto :LABEL
:LABEL Defines a label for use with `goto`. :LABEL
call Calls another batch file or function within the script. call another.bat
pause Pauses execution and waits for the user to press a key. pause
exit Exits the script or command prompt. exit /b 0
for Iterates over files, directories, or variables. for %%i in (*.*) do echo %%i
start Starts a new window or application. start notepad.exe
del Deletes one or more files. del file.txt
copy Copies files from one location to another. copy source.txt destination.txt
move Moves files from one location to another. move file.txt C:\NewFolder
dir Lists files and directories in a specified location. dir C:\
cd / chdir Changes the current directory. cd C:\Folder
md / mkdir Creates a new directory. mkdir NewFolder
rmdir / rd Removes a directory. rmdir Folder
type Displays the content of a text file. type file.txt
find Searches for a text string in a file. find "text" file.txt
cls Clears the command prompt screen. cls
title Sets the title of the command prompt window. title My Batch Script
color Changes the color of the command prompt text and background. color 0A
timeout Pauses execution for a specified time. timeout /t 10
choice Creates a menu for the user to select an option. choice /c ABC
setlocal Starts localization of environment changes. setlocal
endlocal Ends localization of environment changes. endlocal
pushd Saves the current directory and changes to a new one. pushd C:\NewFolder
popd Restores the previous directory saved with `pushd`. popd
xcopy Copies files and directories, including subdirectories. xcopy source destination /s
robocopy Advanced file copy with many options. robocopy source destination
shift Shifts command-line arguments for batch scripts. shift
%1, %2, ... Represents positional parameters passed to the batch script. echo %1
errorlevel Represents the exit code of the last command. if errorlevel 1 echo Error
assoc Displays or modifies file type associations. assoc .txt=txtfile
taskkill Terminates processes by name or PID. taskkill /im notepad.exe
set /p Prompts the user for input. set /p Name=Enter name:




Learn Batch Scripting in 20 Minutes (Coffee Break Series) (Paperback)

Powerful little scripting language that comes builtin to Windows - you can do a lot with batch files (.bat) - from simple file searches, moving files, backups or writing a pacman game!

The coffee break series books are designed for those on the go with never enough time to keep up to date with the ever changing world.














 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.