Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:52:42+00:00 2026-06-02T07:52:42+00:00

OK, I’m needing a batch command, (a FOR loop perhaps?), to check multiple registry

  • 0

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!!!;)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:52:44+00:00Added an answer on June 2, 2026 at 7:52 am

    as you already suspected, for is your friend

    begin with something like this…

    @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
      )
    )
    

    and then change the echo commands with the appropiate handling of the keys

    EDIT1:
    I have edited a bit my code, to show you how to handle the key name.

    1. Note the skip=4 option to skip the first lines of the REG QUERY output;

    2. and the echo %%~na to extract the key name in order to process it and conditionally execute the second REG QUERY

    EDIT2:
    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

    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
    

    then, in the outer for loop, change the echo command to

      call :findkey %%~na
    

    and add the code to proceed with when found

      if "!foundkey!" NEQ "" ( 
        for /f "tokens=*" %%b in ('REG QUERY "%%a" /v "StateFlags0001" 2^>^&1 ^| Find "0x2"') do (
          echo %%b
    

    then add the logic of parsing the list

    :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
    

    remember to also add

    setlocal enabledelayedexpansion

    at the beginning of the BAT file, as it is required for the appropiate variable expansion inside the for loops.

    EDIT3:

    a simpler find method

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

    this simpler for requires !keys! to be separated by blanks, so you will need to prepare !keys! by substituting all blanks by underscores, and the commas by blanks.

    set keys=!keys: =_!
    set keys=!keys:,= !
    

    now, putting all the pieces together,

    @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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.