OK, I’m needing a batch command, (a FOR loop perhaps?), to check multiple registry entries for the existence for the StateFlags0001 key.
If the key doesn’t exist, create it and set its value to 0x2.
If it does exist, ensure that its value is set to 0x2.
I know I can do this the “long way” with some clever IF commands, but I’m wondering if it can be simplified drastically somehow.
Ultimately, I’m wanting the cleanmgr /sagerun:1 command automated via batch so I can do away with the cleanmgr /sageset:1 command because this batch is going to be sent to some friends and family who don’t know much of anything about doing even basic tasks on computers.
It’ll be much easier to have them run a batch file then it would be for me to walk them through the steps that come after/during the cleanmgr sageset:1 command…
Before anyone asks, “Why not simply set everything in the VolumeCaches folder to what you need?”, I’ve omitted several keys because I don’t want them included in the cleanup process so that’s not an option.
Of course, if it’s easier to do the reverse of my request and OMIT them and run the loop on what remains (ie; the keys below) then, by all means, lets do it that way…
Here’s the keys in question:
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"
So, in a nutshell;
Check the above keys for StateFlags0001.
If exist, ensure a value of 0x2.
If not exist, create it and set value to 0x2.
Run cleanmgr /sagerun:1.
Exit.
As always, thanks for the enlightenment!!!;)
as you already suspected,
foris your friendbegin with something like this…
and then change the
echocommands with the appropiate handling of the keysEDIT1:
I have edited a bit my code, to show you how to handle the key name.
Note the
skip=4option to skip the first lines of theREG QUERYoutput;and the
echo %%~nato extract the key name in order to process it and conditionally execute the secondREG QUERYEDIT2:
the check of a string against a list of strings is a bit tricky in BAT files. Here is some code to get you started
initialize a variable to hold the keys you are interested in
then, in the outer for loop, change the
echocommand toand add the code to proceed with when found
then add the logic of parsing the list
remember to also add
setlocal enabledelayedexpansion
at the beginning of the BAT file, as it is required for the appropiate variable expansion inside the
forloops.EDIT3:
a simpler find method
this simpler
forrequires !keys! to be separated by blanks, so you will need to prepare !keys! by substituting all blanks by underscores, and the commas by blanks.now, putting all the pieces together,