/cleanmgr /sagerun in batch scripts

Page 1 of 5 123 ... LastLast

  1. Posts : 3
    Windows 10 21H2
       #1

    /cleanmgr /sagerun in batch scripts


    So I've been working on a batch file that basically is like a homemade junk file cleaner and I really really really want to add
    " cleanmgr /sagerun " to it and it works well, BUT, what happens is that when I let the script run and it gets to that line, after disc cleanup completes, the window(s) hang and the script stops in its tracks until I mouse over the windows, my question is that , is there a command I can add that can detect non-responsive window hangs, or something like that to force the script to keep going?
      My Computer


  2. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    Perhaps you'd like to check that you have used the suggestions in Option 2 of
    Disk Cleanup - TenForumsTutorials
    and, if you need further help, post in that tutorial thread.

    The simplest solution that I've noticed is explained in post #1 of another tutorial thread at
    Create Disk Cleanup All Items Checked Shortcut in Windows 10
    It works and there are no hanging windows afterwards.
    - You need to run the first suggested command to set things up.
    - From then on, you just run the suggested shortcut and there is no further action to take.

    Best of luck,
    Denis
    Last edited by Try3; 18 Aug 2022 at 11:12.
      My Computer


  3. Posts : 3
    Windows 10 21H2
    Thread Starter
       #3

    Try3 said:
    Perhaps you'd like to check that you have used the suggestions in Option 2 of
    Disk Cleanup - TenForumsTutorials
    and, if you need further help, post in that tutorial thread.

    The simplest solution that I've noticed is explained in post #1 of another tutorial thread at
    Create Disk Cleanup All Items Checked Shortcut in Windows 10
    It works and there are no hanging windows afterwards.
    - You need to run the first suggested command to set things up.
    - From then on, you just run the suggested shortcut and there is no further action to take.

    Best of luck,
    Denis
    I'm going to give it a whack with option two, and thats all great content btw. What I'm trying to figure out is how to make that part of this simple batch file I made here : /cleanmgr /sagerun in batch scripts-capture.png
    without needing any human intervention afterwards. The specific issue I'm having is that when disc cleanup initiates, all of the windows hang until I bring them into focus by either clicking on them, hovering over them, or even alt tabbing to bring them into view in order to get it to complete and finish the batch. If that makes sense lol

    - - - Updated - - -



    I uploaded a video to demonstrate lol
      My Computer


  4. Posts : 5,330
    Windows 11 Pro 64-bit
       #4

    This batch script will delete temporary files.

    Code:
    :: Batch Script Created by FreeBooter
    
    @Echo Off & Cls
    
    net sess>nul 2>&1||(powershell start cmd -ArgumentList """/c %~0""" -verb Runas & exit)
    
    Call :IsAdmin
    
    Mode CON LINES=5 COLS=50 & Color 0E
    
    Echo                   PLEASE WAIT... 
    
    :: This command deletes the oldest shadow copy on drive C
    vssadmin delete shadows /for=c: /oldest 2>&1 >nul
    
    ::Creating System Restore point 
    Wmic.exe /Namespace:\\root\default Path SystemRestore Call CreateRestorePoint "Before Deleting Temp Files", 100, 12 
    
    
    ::    CLEANUP STAGE
    Reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files"  /v LastAccess /t REG_DWORD /d "0" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files" /v "StateFlags0064" /t REG_DWORD /d "2" /f 2>&1 >nul
    
    
    
    Cls & Mode CON  LINES=10 COLS=50 & Color 1E & Title Created by FREEBOOTER
    Echo.
    Echo.
    Echo. 
    Echo.
    Echo.
    Echo.
    Echo.
    Echo.
    Echo.
    Echo.
    Echo.
    Echo. 
    Echo.
    Echo.
    Echo.
    Echo           ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» 
    Echo           º DELETING TEMPORARY FILES º  
    Echo           ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
    Echo.
    
    CLEANMGR /sagerun:64 
    
    
    
    Cls
    :: Clear Temporary Folder.
    Cd %TMP% 
    For /f "tokens=*" %%a in ('Dir /b %LOCALAPPDATA%\Temp') do (
    Echo Y | Rd /s /q "%%a" 2>NUL 1>NUL 
    If Exist "%%a" Echo Y | Del /a /f "%%a" 2>NUL 1>NUL 
    )
    Echo. 
    Echo.
    Echo.
    Echo.
    Echo. 
    Echo.
    Echo.
    Echo.
    Echo. 
    Echo.
    Echo.
    Echo.
    Echo. 
    Echo        FINNISH DELETING TEMPORARY FILES 
    Echo.
    Echo.
    ping -n 5 localhost >Nul
    
    
    Cls
    
    Del /a /f /q "%SystemRoot%\Prefetch\*"
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Cls & Mode CON  LINES=12 COLS=80 & Color 1E & Title Created by FREEBOOTER
    
    Cd  %SystemRoot%\System32
    
    Net Start TrustedInstaller 2>NUL 1>NUL
    
    Reg QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName | find /i "Windows 8" >Nul
    If Not Errorlevel 1 (
    Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase
    ) Else (
    Reg QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName | find /i "Windows 8.1" >Nul
    If Not Errorlevel 1 (
    Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase
    ) Else (
    Reg QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName | find /i "Windows 10" >Nul
    If Not Errorlevel 1 (
    Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase
    ) Else (	
    Goto :Event_Log
    ) ) )  
    
    Ping -n 4 localhost >Nul
    
    Goto :Event_Log
    
    
    
    
    :IsAdmin
    Reg query "HKU\S-1-5-19\Environment"
    If Not %ERRORLEVEL% EQU 0 (
     Cls & Mode CON  LINES=5 COLS=48 & Color 0C & Title - WARNING -
     Echo.
     Echo. 
     Echo  YOU MUST HAVE ADMINISTRATOR RIGHTS TO CONTINUE 
     Pause >Nul & Exit
    )
    Cls
    Goto :EOF
    
    
    
    :Event_Log
    Cls
    Mode CON  LINES=33 COLS=85
    For /F "tokens=*" %%G in ('wevtutil.exe el') DO (call :do_clear "%%G")
    Echo.
    Echo                    Event Logs Have Been Cleared!
    Ping -n 5 127.0.0.1 >Nul
    Goto :eof
    :do_clear
    Echo clearing %1
    wevtutil.exe cl %1
    Goto :eof
    /cleanmgr /sagerun in batch scripts Attached Files
      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #5

    Hello @Beasticles,

    At the beginning of your video, a Disk Cleanup for ... box appears, where you can Check or Uncheck items.

    Whenever I perform a Clean Install, I run a Script that I wrote to automatically set the cleanmgr items that I always want Checked or Unchecked. These items are then automatically set in the Registry Key . . .

    Code:
    
    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

    I use number 9999, although it can be any number from 0-65535. This stops the box mentioned above in your video from appearing.

    Then I just run %SystemRoot%\System32\cmd.exe /c cleanmgr /sagerun:9999.

    In the Script, if the DWORD is set to 0, it is Disabled [ Unchecked ], and if the DWORD is set to 2, it is Enabled [ Checked ].

    If you are interested, I can post my Script for you. It is similar to @FreeBooter's Script above but it just uses the Registry Keys as it is just for the cleanmgr items.

    I hope this helps.
    Last edited by Paul Black; 27 Aug 2022 at 13:21.
      My Computer


  6. Posts : 1,223
    W10-Pro 22H2
       #6

    Beasticles said:
    The specific issue I'm having is that when disc cleanup initiates, all of the windows hang until I bring them into focus by either clicking on them, hovering over them, or even alt tabbing to bring them into view in order to get it to complete and finish the batch. If that makes sense lol
    I believe it is only the 'sageset' command that needs user input, so you could run that just the once, setting the clean-up items you want. Then (in theory) that numbered setting is available to use at any time in the future. So you only need the 'sagerun' part in the batch file. Try any number you like, so you know its 'yours'.

    2 points:
    (1) I ran cleanmgr many time to test this, and it seemed to tie itself in knots, sometime just not running at all, others freezing (as you found), so that hovering over the 'just checking' window seemed to allow it to continue. I say seemed, because it could have been a coincidnce - but it happened enough for me to think not.

    (2) Looking in the registry, I found that the values generated by my cleanmgr /sageset commands were only stored with 4 of the 5 numerical digits:
    /cleanmgr /sagerun in batch scripts-cleanmgr.jpg
    I did not go far enough to confirm whether the truncated stored value worked as expected - since my /sagerun commands just execute without hesitation, I can't see what they are doing.

    NB One thing that puzzled me: /sagerun operates on all disks, regardless of what may have been defined in the /sageset command. This was a right nuisance, as it seemed that clearing the recycle bin on drives (that were already empty) seemed to take ages, and I got fed up waiting. It may have been aborting the /sagerun processes that got its knickers in a twist.

    Happy to experiment more if required - Martin
      My Computer


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #7

    Hello @mngerhold [ Martin ],

    mngerhold said:
    I believe it is only the 'sageset' command that needs user input, so you could run that just the once, setting the clean-up items you want. Then (in theory) that numbered setting is available to use at any time in the future. So you only need the 'sagerun' part in the batch file. Try any number you like, so you know its 'yours'.
    I performed extensive testing on this quite a few years back now. This is why I adopt the strategy in my post above, NOT only from the convienience aspect, but knowing which number I allocated, making it easier to find in the Registry Keys.

    I also have a Script that removes the number 9999 for example which I use from the relevant Registry Keys, which makes it easier to maintain and housekeep. I believe that this automatically resets them to the Default state, prior to ANY amendments or changes made.

    mngerhold said:
    (1) I ran cleanmgr many time to test this, and it seemed to tie itself in knots, sometime just not running at all, others freezing (as you found), so that hovering over the 'just checking' window seemed to allow it to continue. I say seemed, because it could have been a coincidnce - but it happened enough for me to think not.
    I get the exactly the same, and indeed, even just placing the mouse over the Disk Cleanup box allows the process to continue.

    mngerhold said:
    (2) Looking in the registry, I found that the values generated by my cleanmgr /sageset commands were only stored with 4 of the 5 numerical digits.

    I did not go far enough to confirm whether the truncated stored value worked as expected...
    YES, it does. I did find this previously, hence why I use the number 9999.

    mngerhold said:
    NB: One thing that puzzled me: /sagerun operates on all disks, regardless of what may have been defined in the /sageset command. This was a right nuisance, as it seemed that clearing the recycle bin on drives (that were already empty) seemed to take ages, and I got fed up waiting. It may have been aborting the /sagerun processes that got its knickers in a twist.
    For me, running on ALL disks is acceptable and beneficial. I have a Script that I run once a week, or whenever I feel like it, that cleans up my computer, along with ANY disks connected internally or externally [ including any USB's if thjey are attached etc ].

    I hope this helps.
      My Computer


  8. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #8

    Beasticles said:
    What I'm trying to figure out is how to make that part of this simple batch file I made here
    I suggest you start with a batch file containing just your CleanMgr command and then add your other commands until you identify the point at which the misbehaviour sets in.

    I prefer & I use the approach taken in post #1 of that other tutorial thread I mentioned at
    Create Disk Cleanup All Items Checked Shortcut in Windows 10
    i.e a once-only setting up command, a SageSet command, followed by routine use of a SageRun command either on its own or in a batch file.
    - It's as simple as that. No further human intervention required at all. The above posts only look complicated because they are trying to explain things to you so you understand what you are doing. The post #1 I linked to just tells you what to do.
    - I use SageSet:1 because it is easy to count.
    - I then use SageRun:1 in a script that I run every month.

    Note that you can backup [export] the relevant Registry key beforehand & afterwards if you wish
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
    but, if you do, make sure you use its .reg export format. Its .txt export format is useless.
    Once you've got the .reg file you can always add a .txt after that to make sure you do not accidentally start it up.
    Whether you've added the extra .txt or not, the exported.reg file can be read easily in Notepad.

    I exported my Registry key after using the SageSet command, trimmed out the irrelevant entries [leaving only the StateFlags & StateFlags0001 entries] and copied it to another computer where I ran it ["merged" it, in the parlance] and was then able to get straight on with my routine SageRun:1 script on that computer. No further human intervention required at all.

    Best of luck,
    Denis
    Last edited by Try3; 27 Aug 2022 at 09:36.
      My Computer


  9. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #9

    Even my previous attempt to explain that it was not complicated looks complicated.

    There are two steps & two steps only:-

    Step 1 - Once-only setting up command in an elevated command window - the dialog containing checkboxes appears for you to select the items you want cleared in future SageRun:1 operations.
    Code:
    cleanmgr.exe /sageset:1

    Step 2 - Routine command to use in an elevated command window or shortcut thereafter whenever you want to clean disks
    Code:
    cleanmgr.exe /sagerun:1


    That's all folks,
    Denis
    Last edited by Try3; 28 Aug 2022 at 12:05.
      My Computer


  10. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #10

    Hello Denis,

    Try3 said:
    Step 1 - Once-only setting up command in an elevated command window.
    Code:
    cleanmgr.exe /sageset:1
    Surely that only sets the Default StateFlags items if you have NOT already changed them [ Checked or Unchecked ] !

    Try3 said:
    Step 2 - Routine command to use thereafter whenever you want to clean disks.
    Code:
    cleanmgr.exe /sagerun:1
    Do you have to hover over the box for it to disappear ?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:52.
Find Us




Windows 10 Forums