当 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 个空格
有什么想法吗?
最佳答案
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
关于windows - 通过批处理 cmd 重命名/删除注册表中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615301/