windows 通过批处理cmd重命名/删除注册表中的键

rqqzpn5f  于 12个月前  发布在  Windows
关注(0)|答案(3)|浏览(146)

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

@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个空格
有什么想法吗?

55ooxyrt

55ooxyrt1#

查询结果以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
a0zr77ik

a0zr77ik2#

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
6ss1mwsb

6ss1mwsb3#

只要加上这个:

set SID=!SID: =!

这将删除所有空间!

相关问题