cmd脚本 - set不会执行

时间:2015-08-05 18:50:46

标签: cmd set

我正在尝试将服务的路径名放入我的cmd脚本中的变量中。当它执行时,我得到设置idmsys = C:\ Windows \ System32 \ spoolsv.exe的行,但它实际上并没有设置变量。任何人都可以解释原因吗?

以下是摘录:

wmic service spooler get pathname | for /f %%i in ('find /I "spool"') do set idmsys=%%i

如果我之后执行echo%idmsys%,则变量为空。

1 个答案:

答案 0 :(得分:0)

运行而不是

for /F "tokens=1" %%i in ('
      wmic service spooler get pathname^, SystemName ^| find /I "spool"
  ') do set "idmsys=%%i"

当命令通过| batch_command传送时,这将实例化一个新的CMD.exe实例。 证明

==>wmic process where "CommandLine like 'wmic%' or CommandLine like 'findstr%' or CommandLine like '%cmd%'" get caption, commandline, ProcessID, ParentProcessID /value | findstr "^"

Caption=cmd.exe
CommandLine="C:\Windows\system32\cmd.exe"
ParentProcessId=3096
ProcessId=4148

Caption=WMIC.exe
CommandLine=wmic  process where "CommandLine like 'wmic%' or CommandLine like 'findstr%' o
r CommandLine like '%cmd%'" get caption, commandline, ProcessID, ParentProcessID /value
ParentProcessId=4148
ProcessId=2084

Caption=findstr.exe
CommandLine=findstr  "^"
ParentProcessId=4148
ProcessId=4596

==>

parallel 甚至 cmd实例中的环境所做的任何更改都不会影响 parallel ,甚至父母一个。的证明

d:\bat>set idmsys
Environment variable idmsys not defined

d:\bat>cmd /E /V /C cd files ^& set "idmsys=!CD!" ^& set idmsys ^& pause
idmsys=d:\bat\files
Press any key to continue . . .

d:\bat>set idmsys
Environment variable idmsys not defined

d:\bat>