简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM