How to declare a variable in batch file
Answer is to use set command.
Set command has 3 options
set => Declares a string variable
set /a => declares an integer variable
set /p => Gets the input from the user
When you want to refer a variable use %var%. e..g
::Declaring a variable, declare a variable using set
::remember not put spaces around "=" while assigning values to the variable
set var1=hello, World!
echo %var1%
set var2= This is NeoTheSaviour
echo %var1%%var2%
How to declare integer variables
::When you set any variable by default its a string, as you have seen in above example
::set has 2 other options, 1) /a which interprate given input as integer
set /a var1=10
set /a var2=5
set /a var3=%var1%+%var2%
echo Sum 0f %var1% and %var2% is %var3%
set /a var3=%var1%*%var2%
echo Product is %var3%
Taking input from user
::second option set has to take input from User /p
echo Addition of Two numbers
set /p var1=Input First Integer
set /p var2=Input Second Integer
set /a var3=%var1%+%var2%
echo Sum is %var3%
You can download above batch file from here
For Loops
Syntax is like below. Note that we use %% while referring iterator.
::For loops and variable
:: For loop uses iterator with %%
for %%a in (a b c d) do echo %%a
::second way of for loop
FOR /L %%i IN (0,1,5) do (
echo %%i
)
No comments:
Post a Comment