moaz Samy
moaz Samy

Reputation: 3

Using a variable in batch file to store a command then execute it

I am new to batch files. Basically what I need to do is something like this

if %1==choice1 set command=dir if %1==choice2 set command=rmdir %command% /q /s

any ideas?

Upvotes: 0

Views: 2493

Answers (1)

cyberponk
cyberponk

Reputation: 1766

Your question is not clear about what you need. Please be more specific. To compare input an save command to variable, use:

if "%1"=="choice1" set "command=dir" 
if "%1"=="choice2" set "command=rmdir /q /s PATH_TO_DELETE" 

To run the command later, just use:

%command%

I highly suggest you put a unique identifier on your variable name, like my_saved_command instead of using a common word like command by itself.

Upvotes: 1

Related Questions