set a command as an environmental variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

set a command as an environmental variable

#1 Post by batchcc » 22 Oct 2015 14:15

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: set a command as an environmental variable

#2 Post by Squashman » 22 Oct 2015 14:22

Yes. Just search the forum for MACRO

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: set a command as an environmental variable

#3 Post by trebor68 » 23 Oct 2015 14:33

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


Post Reply