简体   繁体   English

在批处理文件中使用变量存储命令然后执行

[英]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. 我强烈建议您在变量名上放置一个唯一的标识符,例如my_saved_command,而不要单独使用诸如command这样的常用词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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