Page 1 of 1
set a command as an environmental variable
Posted: 22 Oct 2015 14:15
by batchcc
Hello I would like to know if it is possible to set a cmd command as an environmental variable.
For example if a batch script contains many if statements would it be possible to set the if statement as a variable and run it
Code: Select all
@echo off
Cls
:top
Set /p word
Set "if word=="=var
:: then you can type the following
%var%A-word echo hello
Goto top
Re: set a command as an environmental variable
Posted: 22 Oct 2015 14:22
by Squashman
Yes. Just search the forum for MACRO
Re: set a command as an environmental variable
Posted: 23 Oct 2015 14:33
by trebor68
Here is a example for a batch file.
This batch is waiting for a code "
1234" or "
123456".
Code: Select all
@echo off
setlocal EnableExtensions
:top
echo.
set /p word=Please insert a code word:
set "var=if %word%=="
:: then you can type the following
echo.
%var%1234 (echo Hello user) & goto :here1
%var%123456 (echo Hello user) & goto :here2
echo The code is incorrect.
goto :top
:here1
echo.
echo Here is the part of the program for user with code 1234.
echo.
goto :eof
:here2
echo.
echo Here is the part of the program for user with code 123456.
echo.
goto :eof
Some small changes and the queried code may include spaces.
This batch is waiting for a code "
1234" or "
123456" or "
My day".
Code: Select all
@echo off
setlocal EnableExtensions
:top
echo.
set /p word=Please insert a code word:
:: set "var=if %word%=="
set var=if "%word%"==
:: then you can type the following
echo.
%var%"1234" (echo Hello user) & goto :here1
%var%"123456" (echo Hello user) & goto :here2
%var%"My day" (echo Hello user) & goto :here3
echo The code is incorrect.
goto :top
:here1
echo.
echo Here is the part of the program for user with code 1234.
echo.
goto :eof
:here2
echo.
echo Here is the part of the program for user with code 123456.
echo.
goto :eof
:here3
echo.
echo Here is the part of the program for user with code: My day
echo.
goto :eof