通过批处理cmd

时间:2015-09-16 17:44:36

标签: windows batch-file cmd registry wmic

当Windows 7中的用户登录且配置文件损坏时,他们会获得空白配置文件。我可以通过转到注册表并删除临时配置文件,设置引用计数和状态值来轻松修复。我被困在我的批处理中,因为我从注册表中提取后,一旦我在批处理中调用它,就会在注册表项名称中添加2个空格。如果我回显%SID%,则返回没有可见空间的正确值。注册表确实有%SID%和%SID%.bak

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /F "skip=1 delims=" %%j in ('wmic useraccount where name^="%username%" get SID') do (
  set SID=%%j
  goto :DONE
)
:DONE
echo  SID=%SID%

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%.bak"
pause

批次的结果:

 SID=S-1-5-21-1559011707-81249799-2450556423-1000
Permanently delete the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window
s NT\CurrentVersion\ProfileList\S-1-5-21-1559011707-81249799-2450556423-1000  .b
ak (Yes/No)? y
ERROR: The system was unable to find the specified registry key or value.
Press any key to continue . . .

正如您所看到的那样,输出在SID&之后放置了2个空格。文件扩展名

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

WMI查询结果以UCS-2 LE编码,而不是ANSI。在捕获wmic的输出时,我发现有时在查询中添加一次性列并使用/format:csv来保留格式有用。

@echo off
setlocal

for /f "tokens=2 delims=," %%I in (
    'wmic useraccount where "name='%username%'" get SID^,Status /format:csv'
) do set "SID=%%I"

rem // echo result
set SID

答案 1 :(得分:1)

MarcB是对的,var是不正确的,因为结尾处有空格 您可以在echo中使用引号检查空格并使用delims= "

进行求解
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /F "skip=1 delims= " %%j in ('wmic useraccount where name^="%username%" get SID') do (
  set SID=%%j
  goto :DONE
)
:DONE
echo  SID = '%SID%'
pause
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%.bak"
pause

答案 2 :(得分:0)

只需添加:

  set SID=!SID: =!

这将删除所有空格!

相关问题