好的,我需要一个批处理命令,(可能是一个FOR
循环?),检查多个注册表项是否存在StateFlags0001
密钥。
如果该密钥不存在,请创建该密钥并将其value
设置为0x2
。
如果确实存在,请确保其值设置为0x2
。
我知道我可以通过一些聪明的IF
命令来完成这个“漫长的道路”,但我想知道它是否可以以某种方式大大简化。
最终,我希望通过批处理自动执行cleanmgr /sagerun:1
命令,这样我就可以取消cleanmgr /sageset:1
命令,因为这个批次将被发送给一些不知道的朋友和家人关于在计算机上执行基本任务的任何事情。
让它们运行批处理文件要容易得多,然后让我们完成cleanmgr sageset:1
命令之后/期间的步骤......
在有人询问之前,“为什么不简单地将VolumeCaches
文件夹中的所有内容设置为您需要的内容?”,我省略了几个键,因为我不希望它们包含在清理过程中,因此不是选项。
当然,如果更容易做出我的请求的反转并且OMIT它们并在剩下的东西(即下面的键)上运行循环,那么,无论如何,让我们这样做......
以下是有问题的关键字:
REG QUERY "HKLM\\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files" /v "StateFlags0001" | Find "0x2"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files" /v "StateFlags0001" | Find "0x2"
所以,简而言之;
检查StateFlags0001
的上述键。
如果存在,请确保值0x2
。
如果不存在,请创建它并将值设置为0x2
。
运行cleanmgr /sagerun:1
。
退出。
一如既往,感谢启蒙!!!;)
答案 0 :(得分:1)
正如您所怀疑的那样,for
是您的朋友
从这样的事情开始......
@echo off
for /f "skip=4 tokens=*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\"') do (
echo %%~na
for /f "tokens=*" %%b in ('REG QUERY "%%a" /v "StateFlags0001" 2^>^&1 ^| Find "0x2"') do (
echo %%b
)
)
然后使用适当的键处理
更改echo
命令
<强> EDIT1:强> 我编辑了一下我的代码,向您展示如何处理密钥名称。
请注意skip=4
选项以跳过REG QUERY
输出的第一行;
和echo %%~na
提取密钥名称以便处理密钥名称并有条件地执行第二个REG QUERY
<强> EDIT2:强> 在BAT文件中检查字符串列表是否有点棘手。这里有一些代码可以帮助您入门
初始化变量以保存您感兴趣的键
set keys=Active Setup Temp Folders
set keys=!keys!,Downloaded Program Files
set keys=!keys!,Internet Cache Files
set keys=!keys!,Memory Dump Files
set keys=!keys!,Old ChkDsk Files
set keys=!keys!,Previous Installations
set keys=!keys!,Recycle Bin
set keys=!keys!,Setup Log Files
set keys=!keys!,System error memory dump files
set keys=!keys!,System error minidump files
set keys=!keys!,Temporary Files
set keys=!keys!,Office Setup Files
set keys=!keys!,Temporary Setup Files
set keys=!keys!,Thumbnail Cache
set keys=!keys!,Upgrade Discarded Files
set keys=!keys!,Windows Error Reporting Archive Files
set keys=!keys!,Windows Error Reporting Queue Files
set keys=!keys!,Windows Error Reporting System Archive Files
set keys=!keys!,Windows Error Reporting System Queue Files
set keys=!keys!,Recycle Bin
然后,在外部for循环中,将echo
命令更改为
call :findkey %%~na
并添加代码以便在找到时继续
if "!foundkey!" NEQ "" (
for /f "tokens=*" %%b in ('REG QUERY "%%a" /v "StateFlags0001" 2^>^&1 ^| Find "0x2"') do (
echo %%b
然后添加解析列表的逻辑
:findkey
set keylist=!keys!
set foundkey=
:againlist
for /f "tokens=1* delims=," %%k in ("!keylist!") do (
if "%%k" NEQ "" (
if /i "%*"=="%%k" (
set foundkey=%%k
goto :found
)
)
if "%%l" NEQ "" (
set keylist=%%l
goto :againlist
)
)
:found
goto :eof
记得还要添加
setlocal enabledelayedexpansion
在BAT文件的开头,因为for
循环内的适当变量扩展需要它。
<强> EDIT3:强>
更简单的查找方法
:findkey
set foundkey=
for %%k in (!keys!) do (
set curkey=%%k
set curkey=!curkey:_= !
if /i "!curkey!"=="%*" (
set foundkey=!curkey!
)
)
goto :eof
这个更简单的for
需要!键!由空格分开,所以你需要准备!键!用下划线代替所有空格,用空格替换逗号。
set keys=!keys: =_!
set keys=!keys:,= !
现在,将所有部分放在一起,
@echo off
setlocal enabledelayedexpansion
set keys=Active Setup Temp Folders
set keys=!keys!,Downloaded Program Files
set keys=!keys!,Internet Cache Files
set keys=!keys!,Memory Dump Files
set keys=!keys!,Old ChkDsk Files
set keys=!keys!,Previous Installations
set keys=!keys!,Recycle Bin
set keys=!keys!,Setup Log Files
set keys=!keys!,System error memory dump files
set keys=!keys!,System error minidump files
set keys=!keys!,Temporary Files
set keys=!keys!,Temporary Setup Files
set keys=!keys!,Thumbnail Cache
set keys=!keys!,Upgrade Discarded Files
set keys=!keys!,Windows Error Reporting Archive Files
set keys=!keys!,Windows Error Reporting Queue Files
set keys=!keys!,Windows Error Reporting System Archive Files
set keys=!keys!,Windows Error Reporting System Queue Files
set keys=!keys: =_!
set keys=!keys:,= !
for /f "skip=4 tokens=*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\"') do (
call :findkey %%~na
if "!foundkey!" NEQ "" (
for /f "tokens=*" %%b in ('REG QUERY "%%a" /v "StateFlags0001" 2^>^&1 ^| Find "0x2"') do (
echo %%b
)
)
)
goto :eof
:findkey
set foundkey=
for %%k in (!keys!) do (
set curkey=%%k
set curkey=!curkey:_= !
if /i "!curkey!"=="%*" (
set foundkey=!curkey!
)
)
goto :eof