AutoIt – Run Disk Cleanup, Hide Manager and Notification Window

email me

Download and install AutoIt and AutoIt Editor.

Save code as hiddencleanmgr.au3 for testing. Afterwards, compile to EXE (x86) using AutoIt Editor.

#NoTrayIcon

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3> 

Opt("TrayIconHide", 1)

; Command to run
$AppName = 'cleanmgr.exe /dc /verylowdisk'

; Window to hide
$ParentWindow = GUICreate('Disk Cleanup', 0, 0, 0, 0, 0, $WS_EX_TOOLWINDOW)

; New---hidden window----this will contain the cleanmgr operation
$ChildWindow = GUICreate("WindowTitle", 0, 0, -100, -100, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU), BitOR($WS_EX_DLGMODALFRAME, $WS_EX_TOPMOST), $ParentWindow)

;Sets status of new window
;if this is hidden, you won't be 
;able to hide/control child window
GUISetState(@SW_SHOW)

$ProcPID = Run($AppName,"",@SW_HIDE)
Sleep(2000)
WinWaitActive("Disk Cleanup")


While ProcessExists($ProcPID) = 0
    Sleep(100)
WEnd

;Returns Title by PID - required for Handle
$WindowTitle = _WinGetByPID($ProcPID,1)

;Wait for child to appear
WinWaitActive($WindowTitle)

;Returns handle for child
$ChildHandle = WinGetHandle($WindowTitle)

;Adds child to new parent window
_SetParent($ChildHandle, $ChildWindow)

;Hide child or SW_SHOW child in parent window
WinSetState($ChildHandle, "", @SW_HIDE)
Sleep(2000)

; catch the Disk Space Notification Window
Global $i = 0
DO
Sleep(250)
If WinExists("Disk Space Notification") Then	   ; checks for Notifcation Window
	Run("taskkill /f /im cleanmgr.exe","",@SW_HIDE); cleanup manager process
	Run("taskkill /f /im autoit3.exe","",@SW_HIDE) ; script running uncompiled
	Run("taskkill /f /im hiddencleanmgr.exe","",@SW_HIDE); name of this compiled script
EndIf
$i = $i + 1
UNTIL $i > 9600; 40 minutes runtime


Do
Sleep(10)
Until GuiGetMsg() =-3


Exit

; Functions

Func _SetParent($id_child, $h_parent)	
    If Not IsHWnd($h_parent) Then $h_parent = HWnd($h_parent)
    If Not IsHWnd($id_child) Then $id_child = GUICtrlGetHandle($id_child)
    If DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $id_child, "hwnd", $h_parent) <> 0 Then
        Return 1
    Else
        seterror(1)
        Return 0
    EndIf
EndFunc

Func _WinGetByPID($iPID, $iArray = 1) 	
    Local $aError[1] = [0], $aWinList, $sReturn
    If IsString($iPID) Then
        $iPID = ProcessExists($iPID)
    EndIf
    $aWinList = WinList()
    For $A = 1 To $aWinList[0][0]
        If WinGetProcess($aWinList[$A][1]) = $iPID And BitAND(WinGetState($aWinList[$A][1]), 2) Then
            If $iArray Then
                Return $aWinList[$A][1]
            EndIf
            $sReturn &= $aWinList[$A][1] & Chr(1)
        EndIf
    Next
    If $sReturn Then
        Return StringSplit(StringTrimRight($sReturn, 1), Chr(1))
    EndIf
    Return SetError(1, 0, $aError)
EndFunc   

 


Screenshot

Disk Cleanup Manager running with no window

 

Notes

PowerShell Cleanup

$HKLM = [UInt32] “0x80000002”
$strKeyPath = “SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches”
$strValueName = “StateFlags0065”

$subkeys = gci -Path HKLM:\$strKeyPath -Name
ForEach ($subkey in $subkeys) {
Try {
New-ItemProperty -Path HKLM:\$strKeyPath\$subkey -Name $strValueName -PropertyType DWord -Value 2 -ErrorAction SilentlyContinue| Out-Null
}
Catch {
}
try {
Start-Process cleanmgr -ArgumentList “/sagerun:65” -Wait -NoNewWindow -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
catch {
}
}
ForEach ($subkey in $subkeys) {
Try {
Remove-ItemProperty -Path HKLM:\$strKeyPath\$subkey -Name $strValueName | Out-Null
}
Catch {
}
}

 

tags: Automating disk cleanup, scripting computer  cleanup, MrNetTek