@echo off
setlocal EnableDelayedExpansion
:: This is a commentREM This is another way to commentecho ====================================
echo Windows Batch File Syntax Demo
echo ====================================
:: Variables and user inputset"name=World"set /p "userInput=Please enter your name: "ifnot"%userInput%"==""set"name=%userInput%":: Basic output and variablesecho.
echo Hello, %name%!
echo Current directory: %CD%echo Batch file name: %~nx0echo Full path: %~f0:: Environment variablesecho.
echo System Drive: %SystemDrive%echo Program Files: %ProgramFiles%echo User Profile: %USERPROFILE%:: Arithmetic operationsset /a num1=5set /a num2=3set /a sum=%num1%+%num2%set /a product=%num1%*%num2%echo.
echo Basic Math:
echo%num1% + %num2% = %sum%echo%num1% * %num2% = %product%:: Conditional statementsecho.
echo Conditional Examples:
if%num1%GTR%num2%(echo%num1% is greater than %num2%)else(echo%num1% is not greater than %num2%):: Loop examplesecho.
echo For Loop Example:
for%%i in (12345) do (echo Number: %%i
)echo.
echo Directory Loop Example:
for /d %%d in ("%SystemDrive%\*") do (echo Found directory: %%d
):: Function/Label demonstrationcall:MyFunction"Hello from function!"goto:SkipFunction:MyFunctionecho.
echo Function Demo:
echo Parameter received: %~1exit /b
:SkipFunction:: Error handlingecho.
echo Error Handling Demo:
dir /x nonexistent.file 2>nuliferrorlevel1(echo File not found error detected
):: Delayed expansion exampleset"counter=1"for%%a in (A B C) do (set /a "counter+=1"echo Counter is: !counter! for %%a
):: Choice commandecho.
echo Choice Command Demo:
choice /c YN /m "Do you want to continue"iferrorlevel2goto:EndDemoiferrorlevel1echo You selected Yes
:: Working with filesecho.
echo File Operations Demo:
echo Creating temporary file...
echo Test content > temp.txt
type temp.txt
del temp.txt
:: Command nesting and pipingecho.
echo Command Nesting Demo:
dir|find"DIR">nul&&echo Directories found
:: Random number generationset /a "random_num=%random%%%100+1"echo.
echo Random number between 1 and 100: %random_num%:EndDemoecho.
echo ====================================
echo Demo Complete
echo ====================================
endlocalpause