Windows Batch Scripts

时间:2023-12-15 15:48:56

Some simple examples:

simple.bat

@echo off
set _var1=3
set _var2=5
set /a _result=%_var1%+%_var2%
echo Result is %_result%
mult.bat

@echo off
if "%1" == "" goto :help
if "%2" == "" goto :help
set /a result=%1 * %2
echo Result is %result%
goto :eof :help
echo Usage: mult X Y (Multiply X by Y)
power.bat

@echo off
if "%1" == "" goto :help
if "%2" == "" goto :help
set _result=%1
for /L %%G in (2,1,%2) do set /a "_result*=%1"
echo %1 to the power of %2 is %_result%
goto :eof :help
echo Usage: power X Y (Calculate X to the power of Y.)
functions.bat

@echo off
set /p _num1=Number1:
set /p _num2=Number2:
call :plusfunc %_num1% %_num2%
call :output
call :subfunc %_num1% %_num2%
call :output
goto :eof :plusfunc
setlocal
set /a _num1=%1 + %2
endlocal & set _result=%_num1%
goto :eof :subfunc
setlocal
set /a result=%1 - %2
endlocal & set _result=%result%
goto :eof :output
echo Result is %_result%
goto :eof