1

好的,我需要一个批处理命令(FOR也许是一个循环?)来检查多个注册表项是否存在StateFlags0001密钥。

如果密钥不存在,则创建它并将其设置value0x2. 如果确实存在,请确保将其值设置为0x2

我知道我可以用一些聪明的命令来做这个“很长的路” IF,但我想知道它是否可以以某种方式大大简化。

最终,我希望cleanmgr /sagerun:1命令通过批处理自动化,这样我就可以取消cleanmgr /sageset:1命令,因为这个批处理将被发送给一些对在计算机上执行基本任务一无所知的朋友和家人。让他们运行批处理文件会容易得多,然后我可以引导他们完成cleanmgr sageset:1命令之后/期间的步骤......

在有人问“为什么不简单地将VolumeCaches文件夹中的所有内容设置为您需要的内容?”之前,我已经省略了几个键,因为我不希望它们包含在清理过程中,所以这不是一个选项。

当然,如果更容易执行我的请求并忽略它们并在剩余的内容上运行循环(即下面的键),那么无论如何,让我们这样做......

这是有问题的键:

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。出口。

一如既往,感谢您的启发!!!;)

4

1 回答 1

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: 我编辑了一些代码,向您展示如何处理键名。

  1. 注意skip=4跳过REG QUERY输出第一行的选项;

  2. 以及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

for在 BAT 文件的开头,因为它是循环内适当的变量扩展所必需的。

编辑3:

更简单的查找方法

:findkey
set foundkey=
for %%k in (!keys!) do (
  set curkey=%%k
  set curkey=!curkey:_= !
  if /i "!curkey!"=="%*" (
    set foundkey=!curkey!
  )
) 
goto :eof

这个更简单for需要!keys!用空格隔开,所以你需要准备!keys!将所有空格替换为下划线,逗号替换为空格。

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
于 2012-04-19T16:56:26.873 回答