如何在SET变量中使CMD显示结果而不是字符串

时间:2017-02-19 17:10:25

标签: windows batch-file cmd

我希望实现一个简单的批处理文件,它将重命名当前的本地配置文件夹,备份注册表项然后删除配置文件列表SID键。 因此,允许计算机创建一个非临时的新本地配置文件。

%U%是帐户名称

set a=wmic useraccount where name="%U%" get sid /value

所以当我执行以下(其中%a%是上述命令时):

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%a% /f

它将其解释为:

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\wmic useraccount where name="%U%" get sid /value"

但我想:

"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\SID=S-1-5-21-3519583588-1143172139-1479499458-1001"

如果我拨打%a%,则会显示

  

SID = S-1-5-21-3519583588-1143172139-1479499458-1001

如果我回显%a%,则会显示

wmic useraccount where name="%U%" get sid

如果我只输入%a%,则会显示

  

SID = S-1-5-21-3519583588-1143172139-1479499458-1001

只是解释为什么会发生这种情况会很棒。

2 个答案:

答案 0 :(得分:2)

set没有内置的执行命令和存储结果的能力,正如你想要的那样:

set a=wmic useraccount where name="%U%" get sid /value

相反,一个简单的黑客是:

@echo off
for /f %%A in ('wmic useraccount where "name='%USERNAME%'" get sid /value ^| findstr SID') do ( set %%A )
echo The SID is %SID%

之后,你应该能够:

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%" /f

答案 1 :(得分:0)

与使用反引号作为外部命令的stdout的表达式的Unix不同,批处理中没有这样的语法。您需要做的是在输出行上使用for循环将其分配给变量,如discussed here

for /f "delims=" %%i in ('wmic...') do set A=%%i