User Tools

Site Tools


userspace:cleanup

This script empty every trash of the system, remove old account listed by AccountsToDelete variable and delete main temp files of every users.

Useful when someone complains about “disk space” on a computer shared by several users.

But be careful! This script is quite straightforward and is not still fully tested.

Put autoit3.exe and autoit3 Include\ dir in CLIENT_CONFIG dir.

once.ins:

[Actions]
 
DefVar $ProductId$
DefVar $INST_NTVersion$
DefVar $TmpSuffix$
DefVar $AccountsToDelete$
 
Set $ProductId$ = "cleanup"
 
; list of accounts to delete separated by a '|' 
Set $AccountsToDelete$ = "old_user2|old_user1"
 
Set $INST_NTVersion$ = GetMsVersionInfo
 
ShowBitmap "%ScriptPath%\"  + $ProductId$ + ".png" $ProductId$
 
if ($INST_NTVersion$ >= "6")
    Set $TmpSuffix$ = "AppData\Local\Temp"
else
    Set $TmpSuffix$ = "Local Settings\Temp"
endif
 
 
Message "Remove unused account files. Please wait a while."
ExecWith_account "%ScriptPath%\AutoIt3.exe"
 
Message "Purge Trash bins. Please wait a while."
DOsInAnIcon_trash
 
Message "Remove tmp files. Please wait a while."
ExecWith_temp "%ScriptPath%\AutoIt3.exe"
 
 
 
 
 
[DOsInAnIcon_trash]
rd /S /Q %SystemDrive%\$Recycle.Bin
 
 
 
[ExecWith_account]
#include <File.au3> 
 
Local $AccountsToDelete = StringSplit("$AccountsToDelete$", "|")
Local $AccountDir = "%ProfileDir%"
 
$DirList = _FileListToArray($AccountDir, "*", 2) 
 
If IsArray($DirList) == 0 Then     
    ;MsgBox(0, "", "No Folders Found.")     
    ConsoleWrite("No Folders Found in dir " & $AccountDir & @CRLF)
    Exit 
EndIf 
 
For $i = 1 To $DirList[0]     
    Local $CurrentAccount = $DirList[$i]
    ConsoleWrite("Check " & $CurrentAccount) 
    For $j = 1 To $AccountsToDelete[0]
        Local $AccountToDelete = $AccountsToDelete[$j]
        ;ConsoleWrite(":" & $CurrentAccount & ":" & $AccountToDelete & ":" & @CRLF)
        If $CurrentAccount == $AccountToDelete Then
            ConsoleWrite(": delete it")
            DirRemove($AccountDir & "\\" & $CurrentAccount, 1)
            ExitLoop
        EndIf
    Next
    ConsoleWrite(@CRLF)
Next
 
 
 
[ExecWith_temp]
; remove only content of temp dir
#include <File.au3> 
 
Local $ProfileDir = "%ProfileDir%"
Local $TmpSuffix = "$TmpSuffix$"
 
Local $DirList = _FileListToArray($ProfileDir, "*", 2) 
 
If @error = 1 Then     
    ConsoleWrite("No Folders Found in dir " & $ProfileDir & @CRLF)
    Exit 
EndIf 
 
For $i = 1 To $DirList[0]     
    Local $CurrentAccount = $DirList[$i]
    Local $TmpDir = $ProfileDir & "\" & $CurrentAccount & "\" & $TmpSuffix
    ConsoleWrite("Delete tmp files of " & $TmpDir & @CRLF)
 
    Local $FileList = _FileListToArray($TmpDir, "*") 
    If IsArray($FileList) == 0 Then     
        ConsoleWrite("  no tmp file found" & @CRLF)
        ContinueLoop
    EndIf 
    ConsoleWrite("  nb of files or folders : " & $FileList[0] & @CRLF)
    For $j = 1 To $FileList[0]
        Local $TmpFile = $TmpDir & "\" & $FileList[$j]
        ;ConsoleWrite("  remove file: " & $tmpFile & @CRLF)
        DirRemove($TmpFile, 1)
        FileDelete($TmpFile) 
   Next
   ConsoleWrite(@CRLF)
Next
userspace/cleanup.txt · Last modified: 2021/08/23 08:37 (external edit)