batch-file - 批处理文件: Multiple Commands on one line with Set

标签 batch-file

我有一个框架,我只能通过 PowerShell 运行内容,但我需要运行批处理文件命令。我正在尝试运行 PowerShell 脚本,例如:

cmd /c blah

对于blah我想做一些类似的事情:

set myPath = c:\theDir && if not exist %myPath% mkdir %myPath%

第一次运行它时,这将不起作用,因为 set 命令似乎直到第二行才生效。有什么想法吗?

最佳答案

这是因为 cmd 在一行解析时计算变量,而不是在运行时计算变量。要获得后一种行为,您必须使用延迟扩展:

cmd /c /v:on "set MyPath=C:\theDir&& if not exist "!myPath!" mkdir "!myPath!"

另请注意,set 中的 = 周围不能有空格,否则您将创建一个末尾带有空格的变量名称(即比如说,你的方法无论如何都不会起作用)。

关于batch-file - 批处理文件: Multiple Commands on one line with Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921499/

相关文章:

windows - 批处理文件按修改日期移动文件

windows - 批处理文件 : Find if substring is in string (not in a file)

windows - 如何从批处理文件中打开最大化的java应用程序?

windows - windows脚本可以实时输出吗?

batch-file - 让批处理 "START/WAIT"命令等待程序启动

windows - 在任务计划程序下将 mstsc.exe 作为批处理作业运行

oracle - 请帮助 SQLPLUS?如何使 SQLPLUS 最初使用 DEFINE `OFF` 启动?

batch-file - 批处理 : Open first folder in directory

windows - 批处理文件字符串修改

batch-file - cmd 批处理 - usebackq 为 : string to split altered from comma separated to space separated