Saboath Posted February 18, 2009 Share Posted February 18, 2009 (edited) Hi guys, I wrote this script for work to automate some of the stuff we do before we make a final backup of an old PC and then wipe. The clean up has 3 basic functions, it turns off system restore, removes any profiles that are older than 180 days (6 months) and finally runs a cleanup application that deletes all temporary files on the computer. This is my first in depth script and I think its quit messy and there are probably better ways of doing things! I consider this script in BETA at the moment, the main issue I've noticed with it is that when I tick all the boxes it tries to do everything at once instead of waiting for each part to finish (trying to use WinWaitActive) Here is the script anyway: expandcollapse popup;------------------------------------------------------------------------------------; ; This utility turns off system restore, removes all profiles and then runs cleanup.; ;------------------------------------------------------------------------------------; Dim $Cleanup, $Help #include <GUIConstants.au3> #include <WindowsConstants.au3> ;----------------------; ; Creates the Main GUI; ;----------------------; Func CleanupGUI() #Region ### START Koda GUI section ### Form= $Cleanup = GUICreate("System Cleanup", 254, 226, -1, -1, $WS_EX_TOOLWINDOW) GUISetIcon("C:\WINDOWS\system32\SHELL32.dll", -102) $Group1 = GUICtrlCreateGroup("", 8, 0, 241, 129) $Checkbox2 = GUICtrlCreateCheckbox("Turn off System Restore", 24, 16, 137, 17) $Checkbox1 = GUICtrlCreateCheckbox("Remove Profiles Older Than 150 Days", 24, 40, 201, 17) $Checkbox3 = GUICtrlCreateCheckbox("Run Disk Cleanup", 24, 64, 113, 17) $Run = GUICtrlCreateButton("Run", 24, 96, 75, 25, 0) $Exit = GUICtrlCreateButton("Exit", 144, 96, 75, 25) GUICtrlCreateGroup("", -99, -99, 1, 1) $BETA = GUICtrlCreateLabel("BETA", 104, 128, 32, 17) $Please = GUICtrlCreateLabel("Please Use Only One Option At A Time", 32, 144, 188, 17) $Help = GUICtrlCreateButton("Help", 96, 168, 57, 17, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE DirectoryDelete(); Deletes the folders copied across in DirectoryCopy Function Exit Case $Run DirectoryCopy() If GUICtrlRead($Checkbox2) = $gui_CHECKED Then Sysrestore() WinWaitClose("System Properties") Else EndIf If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $answer = MsgBox(4,"Delete Profiles?", "Do You Really Want to Delete All Profiles older than 180 Days?" & @CRLF & @CRLF & "(This is approximately 6 months)") If $answer = 6 Then Delprof() WinWaitClose("C:\WINDOWS\system32\cmd.exe") Else EndIf Else EndIf If GUICtrlRead($Checkbox3) = $gui_CHECKED Then Cleanup() WinWaitClose("Windows CleanUp!") Else EndIf Case $Exit DirectoryDelete() Exit Case $Help HelpGUI() EndSwitch WEnd EndFunc CleanupGUI() Func DirectoryCopy() DirCopy("\\server\Cleanup", "C:\Temp\Cleanup\") EndFunc Func DirectoryDelete() DirRemove("C:\Temp\Cleanup", 1) DirRemove("C:\IBMTools", 1) EndFunc ;Removes the profiles older than 180 days... Func Delprof() Run("C:\Temp\Cleanup\delfile\delprofiles.bat") EndFunc ;Turn off System Restore Func Sysrestore() Run("C:\WINDOWS\system32\rundll32.exe /d C:\WINDOWS\system32\shell32.dll,Control_RunDLL SYSDM.CPL") WinWaitActive("System Properties") Send("{UP}") Send("!T!A") WinWaitActive("System Restore") Send("!Y") MsgBox(48, "System Restore", "Please wait while System Restore is turned off", 5) Sleep(30000) Send("{ENTER}") WinWaitClose("System Properties") EndFunc ;Runs Cleanup Func Cleanup() Run("C:\Temp\Cleanup\Cleanup\cleanup.bat") WinWaitClose("C:\WINDOWS\system32\cmd.exe") EndFunc Func Complete() WinWaitClose("Windows CleanUp!") MsgBox(0, "System Cleanup", "The System Cleanup has been completed." & @CRLF & @CRLF & "Please remember to turn off Hibernation in the Power Options" & @CRLF & @CRLF & "System Cleanup Will Now Close.") WinWaitClose("Windows CleanUp!") Exit EndFunc ;----------------------; ; Creates the Help GUI; ;----------------------; Func HelpGUI() GUIDelete($Cleanup) #Region ### START Koda GUI section ### Form= $Help = GUICreate("Help", 529, 361, 531, 362, $WS_EX_TOOLWINDOW) $Helpgroup = GUICtrlCreateGroup("Cleanup Help", 8, 8, 497, 321) $Label1 = GUICtrlCreateLabel("This utility performs the following functions:", 16, 32, 204, 17) $Label2 = GUICtrlCreateLabel("- Turns off System Restore", 40, 56, 129, 17) $Label3 = GUICtrlCreateLabel("- Removes Profiles Older than 180 Days", 40, 112, 192, 17) $Label4 = GUICtrlCreateLabel("- Runs a Disk Cleanup.", 40, 168, 113, 17) $Label5 = GUICtrlCreateLabel("This uses keystrokes, please do not interupt.", 64, 80, 214, 17) $Label6 = GUICtrlCreateLabel("This starts a batch script which was written to delete all profiles in the specified date range.", 64, 136, 430, 17) $Label7 = GUICtrlCreateLabel("Cleanup will remove all temporary files from the computer.", 64, 192, 272, 17) $Label8 = GUICtrlCreateLabel("This script will copy a folder from \\wao_services\csc\wagerup to the c:\temp folder", 16, 232, 401, 17) $Label9 = GUICtrlCreateLabel("Terminating the script via the X or the EXIT button will delete this folder as well as the IBMTools folder.", 16, 256, 484, 17) $Button1 = GUICtrlCreateButton("OK", 244, 288, 65, 33, 0) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg1 = GUIGetMsg() Switch $nMsg1 Case $GUI_EVENT_CLOSE ;Exit GUIDelete($Help) CleanupGUI() Case $Button1() GUIDelete($Help) CleanupGUI() EndSwitch WEnd EndFunc I am happy for any input on how to improve this and even bringing the profile deletion internal isntead of relying on an external batch script (which can be provided if you wish) Thanks, Saboath. Edited February 18, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 18, 2009 Share Posted February 18, 2009 Well here's an initial go-over of it, a few things I'll point out.I'm a big fan of using good variable naming conventions when it comes to GUI controls, makes it easier to distinguish them as your script gets more complex. Along that same line, you don't need to assign variables to labels or groups (basically any control you don't interact with again after you create it) unless you're going to make a resizeable gui (and even then I don't know that you HAVE to.At the moment, the functions that run your batch files should wait until the batch file terminates because I switched them from Run to RunWait (can you guess what it does differently than simply Run?). I would suggest that you see if you can't incorporate what the batch files do into your actual script, I think you'll find you can.I changed your function to turn System Restore off into simply writing two registry keys per this website. I tried it on my system and it appeared to turn it off, but I would test it to make sure that it's actually off as far as your needs are concerned before you rely on it.All in all, not a bad first in-depth script! There are little nuances to it (I ran Tidy on it [which is in SciTE under the Tools] and that made the formatting look a lot better), but you're well on your way to some quality material, especially since you're such a gratuitous commenter!One thing you may also consider adding is disabling either the GUI window or the different controls when you click the buttons, just to keep your users from getting click-happy, and then re-enabling them when that button's procedures are done.expandcollapse popup;------------------------------------------------------------------------------------; ; This utility turns off system restore, removes all profiles and then runs cleanup.; ;------------------------------------------------------------------------------------; #include <GUIConstants.au3> #include <WindowsConstants.au3> Dim $Cleanup, $Help CleanupGUI() ;----------------------; ; Creates the Main GUI; ;----------------------; Func CleanupGUI() #Region ### START Koda GUI section ### Form= $guiCleanup = GUICreate("System Cleanup", 254, 226, -1, -1, $WS_EX_TOOLWINDOW) GUISetIcon("C:\WINDOWS\system32\SHELL32.dll", -102) GUICtrlCreateGroup("", 8, 0, 241, 129) $chkTurnOffSysRestore = GUICtrlCreateCheckbox("Turn off System Restore", 24, 16, 137, 17) $chkRemoveProfiles = GUICtrlCreateCheckbox("Remove Profiles Older Than 150 Days", 24, 40, 201, 17) $chkDiskCleanup = GUICtrlCreateCheckbox("Run Disk Cleanup", 24, 64, 113, 17) $btnRun = GUICtrlCreateButton("Run", 24, 96, 75, 25, 0) $btnExit = GUICtrlCreateButton("Exit", 144, 96, 75, 25) GUICtrlCreateLabel("BETA", 104, 128, 32, 17) GUICtrlCreateLabel("Please Use Only One Option At A Time", 32, 144, 188, 17) $btnHelp = GUICtrlCreateButton("Help", 96, 168, 57, 17, 0) GUISetState(@SW_SHOW) #EndRegion ### START Koda GUI section ### Form= While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE DirectoryDelete(); Deletes the folders copied across in DirectoryCopy Function Exit Case $btnRun DirectoryCopy() If GUICtrlRead($chkTurnOffSysRestore) = $GUI_CHECKED Then Sysrestore() EndIf If GUICtrlRead($chkRemoveProfiles) = $GUI_CHECKED Then $answer = MsgBox(4, "Delete Profiles?", "Do You Really Want to Delete All Profiles older than 180 Days?" & @CRLF & @CRLF & "(This is approximately 6 months)") If $answer = 6 Then Delprof() WinWaitClose("C:\WINDOWS\system32\cmd.exe") EndIf EndIf If GUICtrlRead($chkDiskCleanup) = $GUI_CHECKED Then Cleanup() WinWaitClose("Windows CleanUp!") EndIf Case $btnExit DirectoryDelete() Exit Case $btnHelp HelpGUI() EndSwitch WEnd EndFunc ;==>CleanupGUI #Region GUI Functions ;Runs Cleanup Func Cleanup() RunWait("C:\Temp\Cleanup\Cleanup\cleanup.bat") EndFunc ;==>Cleanup Func Complete() WinWaitClose("Windows CleanUp!") MsgBox(0, "System Cleanup", "The System Cleanup has been completed." & @CRLF & @CRLF & "Please remember to turn off Hibernation in the Power Options" & @CRLF & @CRLF & "System Cleanup Will Now Close.") WinWaitClose("Windows CleanUp!") Exit EndFunc ;==>Complete Func DirectoryCopy() DirCopy("\\server\Cleanup", "C:\Temp\Cleanup\") EndFunc ;==>DirectoryCopy Func DirectoryDelete() DirRemove("C:\Temp\Cleanup", 1) DirRemove("C:\IBMTools", 1) EndFunc ;==>DirectoryDelete ;Removes the profiles older than 180 days... Func Delprof() RunWait("C:\Temp\Cleanup\delfile\delprofiles.bat") EndFunc ;==>Delprof ;Turn off System Restore Func Sysrestore() RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore", "DisableSR", "Reg_Dword", 1) ; 1 to disable, 0 to enable RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\sr", "start", "Reg_Dword", 4) ; 4 to disable, 0 to enable EndFunc ;==>Sysrestore #EndRegion ;----------------------; ; Creates the Help GUI; ;----------------------; Func HelpGUI() GUIDelete($Cleanup) #Region ### START Koda GUI section ### Form= $Help = GUICreate("Help", 529, 361, 531, 362, $WS_EX_TOOLWINDOW) $Helpgroup = GUICtrlCreateGroup("Cleanup Help", 8, 8, 497, 321) $Label1 = GUICtrlCreateLabel("This utility performs the following functions:", 16, 32, 204, 17) $Label2 = GUICtrlCreateLabel("- Turns off System Restore", 40, 56, 129, 17) $Label3 = GUICtrlCreateLabel("- Removes Profiles Older than 180 Days", 40, 112, 192, 17) $Label4 = GUICtrlCreateLabel("- Runs a Disk Cleanup.", 40, 168, 113, 17) $Label5 = GUICtrlCreateLabel("This uses keystrokes, please do not interupt.", 64, 80, 214, 17) $Label6 = GUICtrlCreateLabel("This starts a batch script which was written to delete all profiles in the specified date range.", 64, 136, 430, 17) $Label7 = GUICtrlCreateLabel("Cleanup will remove all temporary files from the computer.", 64, 192, 272, 17) $Label8 = GUICtrlCreateLabel("This script will copy a folder from \\wao_services\csc\wagerup to the c:\temp folder", 16, 232, 401, 17) $Label9 = GUICtrlCreateLabel("Terminating the script via the X or the EXIT button will delete this folder as well as the IBMTools folder.", 16, 256, 484, 17) $Button1 = GUICtrlCreateButton("OK", 244, 288, 65, 33, 0) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) #EndRegion ### START Koda GUI section ### Form= While 1 $nMsg1 = GUIGetMsg() Switch $nMsg1 Case $GUI_EVENT_CLOSE ;Exit GUIDelete($Help) CleanupGUI() Case $Button1() GUIDelete($Help) CleanupGUI() EndSwitch WEnd EndFunc ;==>HelpGUI Link to comment Share on other sites More sharing options...
Saboath Posted February 18, 2009 Author Share Posted February 18, 2009 Hey, Thanks for a fast reply and for the suggestions on the RunWait command, as i'm still new I wasn't aware of it! I have seen the registry entries before but from personal experiance, when you change the registry, the changes don't normally come into effect until next time you reboot your computer whereas turning it off within the system properties turns it off straight away and frees up the disk space that is being used. The batch script is here: expandcollapse popup@echo off setlocal call :GETDATEPARTS "%date%" call :SUBTRACTDAYS 180 set cutOff=%yy%%mm%%dd% set baseDir="C:\Docume~1" if not "%~1"=="" set baseDir=%~1 if not exist "%baseDir%" echo %~1 directory does not exist&goto :EOF pushd "%baseDir%" for /f "tokens=*" %%a in ('dir /b /ad 2^>NUL') do call :PROCESS "%%a" %%~ta popd goto :EOF :PROCESS call :GETDATEPARTS "%~2" if /i "%yy%%mm%%dd%" GEQ "%cutOff%" goto :EOF echo Deleting directory %~1... rd /s /q "%~1" goto :EOF :GETDATEPARTS set dt=%~1 set tok=1-3 if "%dt:~0,1%" GTR "9" set tok=2-4 set yyyy= for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do ( for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c ) if not "%yyyy%"=="" set yy=%yyyy% if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%)) if 1%mm% LSS 100 set mm=0%mm% if 1%dd% LSS 100 set dd=0%dd% goto :EOF :SUBTRACTDAYS set dayCnt=%1 if "%dayCnt%"=="" set dayCnt=1 REM Substract your days here set /A dd=1%dd% - 100 - %dayCnt% set /A mm=1%mm% - 100 :CHKDAY if /I %dd% GTR 0 goto DONESUBTRACT set /A mm=%mm% - 1 if /I %mm% GTR 0 goto ADJUSTDAY set /A mm=12 set /A yy=%yy% - 1 :ADJUSTDAY if %mm%==1 goto SET31 if %mm%==2 goto LEAPCHK if %mm%==3 goto SET31 if %mm%==4 goto SET30 if %mm%==5 goto SET31 if %mm%==6 goto SET30 if %mm%==7 goto SET31 if %mm%==8 goto SET31 if %mm%==9 goto SET30 if %mm%==10 goto SET31 if %mm%==11 goto SET30 REM ** Month 12 falls through :SET31 set /A dd=31 + %dd% goto CHKDAY :SET30 set /A dd=30 + %dd% goto CHKDAY :LEAPCHK set /A tt=%yy% %% 4 if not %tt%==0 goto SET28 set /A tt=%yy% %% 100 if not %tt%==0 goto SET29 set /A tt=%yy% %% 400 if %tt%==0 goto SET29 :SET28 set /A dd=28 + %dd% goto CHKDAY :SET29 set /A dd=29 + %dd% goto CHKDAY :DONESUBTRACT if /I %mm% LSS 10 set mm=0%mm% if /I %dd% LSS 10 set dd=0%dd% goto :EOF I have no idea where to start with this.. also, I found this script online, so I can take no responsibility for writing it! Thanks again! Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 18, 2009 Share Posted February 18, 2009 (edited) Hey, Thanks for a fast reply and for the suggestions on the RunWait command, as i'm still new I wasn't aware of it! I have seen the registry entries before but from personal experiance, when you change the registry, the changes don't normally come into effect until next time you reboot your computer whereas turning it off within the system properties turns it off straight away and frees up the disk space that is being used. I have no idea where to start with this.. also, I found this script online, so I can take no responsibility for writing it! Thanks again! Saboath How about this? It's kindofa standalone script at the moment, but you should be able to test with it to see that it does what you want, then integrate it into your gui. #Include <Date.au3> #Include <File.au3> $FileList=_FileListToArray("C:\Documents And Settings", "*", 2) If @Error=1 Then MsgBox (0,"","No Files\Folders Found.") Exit EndIf For $x = 1 to $FileList[0] $t = FileGetTime ("C:\Documents And Settings\" & $FileList[$x], 2) $varTimeStamp = $t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5] $varTimeDifference = _DateDiff ("D", $varTimeStamp, _NowCalc()) MsgBox (0, "Time Difference", "It's been " & $varTimeDifference & ' days since the profile "' & $FileList[$x] & '" was last accessed.') If $varTimeDifference >= 180 Then ;~ DirRemove ("C:\Documents And Settings\" & $FileList[$x], 1) EndIf Next I added a MsgBox to illustrate how long it's been since that folder was last accessed, and commented out the DirRemove line so you didn't inadvertently start deleting things... Edited February 18, 2009 by exodius Link to comment Share on other sites More sharing options...
exodius Posted February 18, 2009 Share Posted February 18, 2009 And here's a better way to do the System Restore, since you said setting that registry key doesn't quite do the trick: #include <GuiTab.au3> Run("C:\WINDOWS\system32\rundll32.exe /d C:\WINDOWS\system32\shell32.dll,Control_RunDLL SYSDM.CPL") WinWait ("System Properties") $hSysPropWND = WinGetHandle ("System Properties") $hTabCTRL = ControlGetHandle ($hSysPropWND, "", "SysTabControl321") _GUICtrlTab_ClickTab ($hTabCTRL, 4) ControlClick ($hSysPropWND, "", "Button1") $SysPropText = WinGetText ($hSysPropWND) $hBtnCTRL = ControlGetHandle ($hSysPropWND, "", "Button5") ControlClick ($hSysPropWND, "", $hBtnCTRL) If StringInStr ($SysPropText, "Monitoring") Then WinWait ("System Restore") $hSysRestWND = WinGetHandle ("System Restore") $hBtnCTRL = ControlGetHandle ($hSysRestWND, "", "Button1") ControlClick ($hSysRestWND, "", $hBtnCTRL) WinWaitClose ($hSysRestWND) EndIf WinWaitClose ($hSysPropWND) Now I hope you're going through and looking up what all of these different functions do... Link to comment Share on other sites More sharing options...
Saboath Posted February 18, 2009 Author Share Posted February 18, 2009 Wow.. ... umm.. Wow! Ok... Exodius, you rock I'm gonna go test these bits now and then integrate them if successful! Link to comment Share on other sites More sharing options...
exodius Posted February 18, 2009 Share Posted February 18, 2009 Wow..... umm.. Wow!Ok... Exodius, you rock I'm gonna go test these bits now and then integrate them if successful!Well thanks, post back if you have any other questions/problems. Link to comment Share on other sites More sharing options...
Saboath Posted February 18, 2009 Author Share Posted February 18, 2009 (edited) Ok... I have tested both these scripts and heres my findings: The directory deletion one tells me every folder has been access either 0 days or 1 days ago (instead of the true difference) The System restore one tells me there is no such Tab Control: _GUICtrlTab_ClickTab I am using ScITE4AutoIt3... I had better go check for a difference version just in case! *EDIT* Ok, I had version 3.2, updated to 3.3 and it tells me that ClickTab is now valid! Edited February 18, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 18, 2009 Share Posted February 18, 2009 Ok... I have tested both these scripts and heres my findings:The directory deletion one tells me every folder has been access either 0 days or 1 days ago (instead of the true difference)The System restore one tells me there is no such Tab Control: _GUICtrlTab_ClickTabI am using ScITE4AutoIt3... I had better go check for a difference version just in case!*EDIT*Ok, I had version 3.2, updated to 3.3 and it tells me that ClickTab is now valid! <3 for the checking for an update thing, you're like the best new person to the forums ever.Are you able to actually check that on a computer that would have profiles that weren't accessed today? @ home here my profiles unfortunately all have been written to today (because my profile and the system ones are the only ones on it)... I won't have access to a system that could have profiles on it that haven't been used in a while until I get to work tomorrow.Now if you want a more in-depth breakdown of the difference, (like Hours, Minutes, Seconds, etc.) you'll want to look at that _DateDiff function, you'll change the "D" to various different things to get the other values, I figured Days would be fitting since that's what you said you wanted to check for. Link to comment Share on other sites More sharing options...
Saboath Posted February 19, 2009 Author Share Posted February 19, 2009 (edited) Yeah, I have been trying it on my computer (with the delete line commented) and I have profiles on my work computer from 2006. The only real issue I see is here: $varTimeStamp = $t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5] Reading that, I have "YYYY/MM/DD HH:MM" _DateDiff requires "YYYY/MM/DD HH:MM:SS" Although as far as I can see, FileGetTime only returns "YYYY/MM/DD HH:MM" (tried to add a [6] on the end and it errored) *EDIT* Ok, I was wrong... The array is a single dimension array containing six elements: $array[0] = year (four digits) $array[1] = month (range 01 - 12) $array[2] = day (range 01 - 31) $array[3] = hour (range 00 - 23) $array[4] = min (range 00 - 59) $array[5] = sec (range 00 - 59) Notice that return values are zero-padded. *EDIT* Again Ok.. found the problem: $t = FileGetTime ("C:\Documents And Settings\" & $FileList[$x], 2) Needs to be: $t = FileGetTime ("C:\Documents And Settings\" & $FileList[$x], 0) Edited February 19, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 19, 2009 Share Posted February 19, 2009 (edited) Ok.. found the problem: $t = FileGetTime ("C:\Documents And Settings\" & $FileList[$x], 2) Needs to be: $t = FileGetTime ("C:\Documents And Settings\" & $FileList[$x], 0) Ah, I see, yeah in my putting together of the original script I thought that the Accessed property made more sense, but if the Modified property works for you then good! Are you going to try to port that Cleanup batch file to AutoIt as well? Edited February 19, 2009 by exodius Link to comment Share on other sites More sharing options...
Saboath Posted February 19, 2009 Author Share Posted February 19, 2009 (edited) Yeah, unfortunately the accessed date was either today or yesterday (go figure) but I actually needed the last time it was modified (good indicator of how long since that person logged into the machine)Ok, the cleanup part is actually an application from http://www.stevengould.org/The batch script simply imports some registry keys so it does certain things without us having to manually set the options each time we run it.I would love to bring this part internally too so I am not relying on any external applications, so here is basically what it does:Empties Recycle Bins ( FileRecycleEmpty("C:\") ? )Deletes CookiesDeletes Prefetch FilesDeletes Temporary Internet FilesAnd any file matching the following:~*.**.*~*.bak*.chk*.tmpindex.datThe above has been known to clear up as much as 5G of hard drive space As a bonus, is it possible for it to tally up how much space it has saved to show at the end? Not really important, but nice to know the script is working! Edited February 19, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 19, 2009 Share Posted February 19, 2009 (edited) I see I see. Well that being the case, it should be pretty easy to replace the batch file portion using RegWrite...As far as the Cleanup! program is concerned, I agree wholeheartedly that you should write out your own process for it, it would be good experience!The cookies and temp internet files should be easy enough to clear out after you've removed those old profiles, just get the same _FileListToArray after the old ones are gone and go through each deleting those folders...According to this article, you should be able to just delete all of the files in C:\Windows\Prefetch to clear those.There are several examples of a recursive file search on the forums, I tried this one out and it seemed to work pretty well. It should let you delete all of those different extensions that you listed Here's some code for how to use it:(bear in mind that you'll need to copy the function from that post and paste it into your script somewhere, it's not a built-in function of AutoIt)$array = RecurseDir("C:\", "*.tmp") For $x = 1 to $array[0] MsgBox (0, "", $array[$x]) NextYou could probably get an estimate of how much space you recovered if you do a FileGetSize on every file that you delete and keep adding that to a variable that starts at 0 when you start and then return it at the end when you're done. Edited February 19, 2009 by exodius Link to comment Share on other sites More sharing options...
Saboath Posted February 23, 2009 Author Share Posted February 23, 2009 (edited) Hey Exodius, thanks again. Here is where I am at: Done: Recycle Bin, and Prefetch Files Recursive File Search: I have told it to search for the following: c $array = RecurseDir("C:\", "*.tmp,~*.*,*.*~,*.bak,*.chk,index.dat") *EDIT* - Ok, I found it was the ~*.* and *.*~ that was causing this. not sure why.... But in the test's I run, it is returning thing such as: C:\WINDOWS\WindowsShell.Manifest Im pretty sure I don't want to delete that! I am also having trouble with the cookies and temporary internet files though. Using the _FileListToArray on C:\Documents and Settings only seems to return the first level of folders (the profile names) How do I tell it to search deeper until it finds the correct folder: C:\Documents and Settings\PROFILE\Local Settings\Temporary Internet Files Is there a way to wildcard the the profile? Thanks again! Sab. Edited February 23, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 23, 2009 Share Posted February 23, 2009 (edited) Well, I'm not exactly sure why the ~*.* and *.*~ is returning those... Interestingly if you do a search with just those then you pull up only the results that you don't want (at least that's what it did for me. I guess I would say omit them from your search unless you're positive they need to be included...As for the second issue, since you know that those temp internet files are always going to be in that location under each profile, could you do a _FileListToArray on the Documents and Settings folder to get a list of the profiles then just go to each one? Something like:#include <File.au3> $array = _FileListToArray ("C:\Documents and Settings") For $x = 1 to $array[0] MsgBox (0, "", "C:\Documents and Settings\" & $array[$x] & "\Local Settings\Temporary Internet Files") Next**Edit - There may be something in the way of an explanation in the the HelpFile under the FileFindFirstFile function... (which is what that recursive search uses) Edited February 23, 2009 by exodius Link to comment Share on other sites More sharing options...
Saboath Posted February 23, 2009 Author Share Posted February 23, 2009 See, I knew there was an easy solution but I was just looking at it from the wrong angle! Ok, heres my next puzzler for you.. i'm reading help files here but nothing has come to light yet: $finalsize = 0 For $x = 1 to $array[0] $size = FileGetSize($array[$x]) $finalsize = $finalsize + $size $totalsize = $finalsize / 1048576 Unfortunately returns something like: File size is 1374 Total is: 1374 Total Size is: 0.00131034851074219Mb Which is actually 100% correct.. but umm.. can I just show 0.00 and keep it to 2 decimal places? Cheers, Sab. Link to comment Share on other sites More sharing options...
exodius Posted February 23, 2009 Share Posted February 23, 2009 See, I knew there was an easy solution but I was just looking at it from the wrong angle! Ok, heres my next puzzler for you.. i'm reading help files here but nothing has come to light yet: $finalsize = 0 For $x = 1 to $array[0] $size = FileGetSize($array[$x]) $finalsize = $finalsize + $size $totalsize = $finalsize / 1048576 Unfortunately returns something like: File size is 1374 Total is: 1374 Total Size is: 0.00131034851074219Mb Which is actually 100% correct.. but umm.. can I just show 0.00 and keep it to 2 decimal places? Cheers, Sab. See now, this should do it, but I'm thinking since your two decimal places we're rounding to are 0's then it's just showing it as 0 instead of 0.00. If you wanted to be able to show just the 0.00 I'm think you'd have to check for that, otherwise this should work. MsgBox (0, "", Round (0.00131034851074219, 2)) Link to comment Share on other sites More sharing options...
Saboath Posted February 23, 2009 Author Share Posted February 23, 2009 Ok, that worked a treat. Which in theory means.. everything works. I have about a dozen little AU3 scripts now outputting message boxes.. time to bring them all into the central program and get the file size tally working with everything together. Once its all complete, Ill post again with my findings! Link to comment Share on other sites More sharing options...
Saboath Posted February 25, 2009 Author Share Posted February 25, 2009 (edited) Ok..My next question relates to an earlier one, but because we have brought stuff internally the situation has changed.When I click one button I want it to run the cleanup script which I have consisting of several different functions-Delete CookesDelete Temporary Internet FilesDelete PrefetchEmpty Recycle BinsDelete Temporary FilesNow, previously I was using the runwait function.. but obviously that only works with external applications/batch scripts etc.So either I do it this way:If GUICtrlRead($chkDiskCleanup) = $GUI_CHECKED ThenDeleteCookies()DeleteTIF()etcOr I do:Run Function which then runs the next function which then runs the next function (chained along)To me it made sense to do it the chained way, so each was setup in basically this fashion: ; Deletes Cookies Func DeleteCookies() $array = _FileListToArray ("C:\Documents and Settings") For $x = 1 to $array[0] $size = DirGetSize("C:\Documents and Settings\" & $array[$x] & "\Cookies") $totalsize = $totalsize + $size FileDelete ("C:\Documents and Settings\" & $array[$x] & "\Cookies") Next DeleteTIF() EndFunc;==>Deletes CookiesBut unfortunately it just tried to do everything at once and I left it running overnight and while some parts seemed to have worked, other parts did not.One of the parts it did not complete was the prefetch which I have written as follows: ; Delete Prefetch Files Func DelPref() $PrefFiles = _FileListToArray (@WindowsDir & "\Prefetch") For $x = 1 to $PrefFiles[0] $size = FileGetSize($PrefFiles[$x]) $totalsize = $totalsize + $size FileDelete ($PrefFiles[$x]) Next DeleteBin() EndFunc;==> Delete Prefetch FilesTo me it looks fine but eh... I'm no expert (yet) Edited February 25, 2009 by Saboath Link to comment Share on other sites More sharing options...
exodius Posted February 25, 2009 Share Posted February 25, 2009 (edited) You can actually do it either way, both work. From the standpoint of keeping things orderly, I guess I'd suggest the first method though rather than chaining them together, it just puts all your function calls in one place instead of having to go through each function to find the call for the next one... But it's up to you. To debug what's going on, I'd try the Tools --> "Trace: Add Trace Lines" in SciTE. It will go through each line of your script and add a consolewrite with the contents of each line so you can see the progress of your script when you run it from SciTE. That should give you a pretty clear idea as to whether it's running that part or not, and you can just "undo" to remove them. Oh and as for that Prefetch function, see if this illustrates what you've got wrong. #include <File.au3> $totalsize = 0 DelPref() ; Delete Prefetch Files Func DelPref() $PrefFiles = _FileListToArray (@WindowsDir & "\Prefetch") For $x = 1 to $PrefFiles[0] $totalsize += FileGetSize(@WindowsDir & "\Prefetch\" & $PrefFiles[$x]) ;~ FileDelete (@WindowsDir & "\Prefetch\" & $PrefFiles[$x]) MsgBox (0, "", @WindowsDir & "\Prefetch\" & $PrefFiles[$x] & @CRLF & $totalsize) Next ;~ DeleteBin() EndFunc;==> Delete Prefetch Files Edited February 25, 2009 by exodius Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now