snooz Posted June 9, 2005 Share Posted June 9, 2005 (edited) Hi Guys, I am looking to push a package through SMS to clean up client machines %TEMP% directory and defrag the drives. I found a script for the defrag piece but I have been unable to locate anything for clearing out the %TEMP% directory. I tried using a batch file but for some reason it errors out on the deltree command (even though it works) and the reports in SMS are showing a failure. The builds have %TEMP% located at C:\Windows\Temp. Here is the defrag script I am using just in case you need it; RunWait(@ComSpec & ' /c defrag c:') RunWait(@ComSpec & ' /c defrag d:') RunWait(@ComSpec & ' /c defrag e:') RunWait(@ComSpec & ' /c defrag f:') Edited June 9, 2005 by snooz Link to comment Share on other sites More sharing options...
blindwig Posted June 9, 2005 Share Posted June 9, 2005 Windows XP does not have DELTREE. Use RD /S /Q instead. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 unable to locate anything for clearing out the %TEMP% directory. I tried using aCheck this in the help file: @TempDir, FileDelete(), DirRemove(). You will have to "walk" through the list of files/directories in %TEMP% and delete them with FileDelete or DirRemove.Something like this: not tested !!FileChangeDir(@TempDir) $search = FileFindFirstFile("*.*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop if StringInStr(FileGetAttrib($file),"d") then DirRemove($file, 1);- !! Recursive delete !! else FileDelete($file) endif WEnd ; Close the search handle FileClose($search)CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 Windows XP does not have DELTREE. Use RD /S /Q instead.<{POST_SNAPBACK}>Ive added deltree into our builds manually. Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 (edited) Check this in the help file: @TempDir, FileDelete(), DirRemove(). You will have to "walk" through the list of files/directories in %TEMP% and delete them with FileDelete or DirRemove.Something like this: not tested !!FileChangeDir(@TempDir) $search = FileFindFirstFile("*.*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop if StringInStr(FileGetAttrib($file),"d") then DirRemove($file, 1);- !! Recursive delete !! else FileDelete($file) endif WEnd ; Close the search handle FileClose($search)CheersKurt<{POST_SNAPBACK}>Kurt thanks a ton this is exactly what I was looking for. I went ahead and tested it and it works but I keep getting the following error;D: is a Windows system folder and is required for Windows to run properly. It cannot be deleted.The problem is the error stops the script from completing. Here is my updated script after your help.FileChangeDir(@TempDir)$search = FileFindFirstFile("*.*") ; Check if the search was successfulIf $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") ExitEndIfWhile 1 $file = FileFindNextFile($search) If @error Then ExitLoop if StringInStr(FileGetAttrib($file),"d") then DirRemove($file, 1);- !! Recursive delete !! else FileDelete($file) endifWEnd; Close the search handleFileClose($search)RunWait(@ComSpec & ' /c defrag c:')RunWait(@ComSpec & ' /c defrag d:')RunWait(@ComSpec & ' /c defrag e:')RunWait(@ComSpec & ' /c defrag f:') Edited June 9, 2005 by snooz Link to comment Share on other sites More sharing options...
blindwig Posted June 9, 2005 Share Posted June 9, 2005 Kurt thanks a ton this is exactly what I was looking for. I went ahead and tested it and it works but I keep getting the following error;D: is a Windows system folder and is required for Windows to run properly. It cannot be deleted.The problem is the error stops the script from completing.<{POST_SNAPBACK}>You need to exclude the '.' and '..' directories, or bad things will happen My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 Kurt thanks a ton this is exactly what I was looking for. I went ahead and tested it and it works but I keep getting the following error;your welcome.CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 You need to exclude the '.' and '..' directories, or bad things will happen<{POST_SNAPBACK}>blindwig I am not following your post, what do you mean? Link to comment Share on other sites More sharing options...
Blue_Drache Posted June 9, 2005 Share Posted June 9, 2005 (edited) blindwig I am not following your post, what do you mean?<{POST_SNAPBACK}>Ok, in DOS, what happens when you type "CD .."You change back a directory, right?Ok, now type in "Deltree .."guess what happenes?You try to prune off the directory one back from where you're at.If you're on the root drive, it will crash. There's nowhere to go back to.Another worst case scenario is that you will inadvertantly prune all directories and files on the drive. If you wind up doing that, may I recommend Linux?Also, in DOS, "CD ." (useless command, but 'eh, it works for batch files)Changes you to the current directory.C:\>CD C:\BLAH\DIDDY\BLAH C:\BLAH\DIDDY\BLAH\>ECHO Y | DELTREE . Are you sure?(Y/N)Y Deleting..... Current directory is no longer valid .... Edited June 9, 2005 by Blue_Drache Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 You need to exclude the '.' and '..' directories, or bad things will happenIn general: really good point!! Allways try to think the "unthinkable"However, in my sample code DirRemove() will not delete the current directory "." or the parent directory ".." as we changed the current directory with FileChangeDir(), which prevents it from beeing deleted. DirRemove(".") will return 0 (== Error).CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 In general: really good point!! Allways try to think the "unthinkable"However, in my sample code DirRemove() will not delete the current directory "." or the parent directory ".." as we changed the current directory with FileChangeDir(), which prevents it from beeing deleted. DirRemove(".") will return 0 (== Error).CheersKurt<{POST_SNAPBACK}>I have been playing around with the code and I can't seem to suppress the error message;D: is a Windows system folder and is required for Windows to run properly. It cannot be deleted.Trying to figure out what is causing this any ideas? Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 D: is a Windows system folder and is required for Windows to run properly. It cannot be deleted.D: ?? why is it getting to this point? Could you provide a list of the files your script finds ?Dim $output = @TempDir & @CRLF FileChangeDir(@TempDir) $search = FileFindFirstFile("*.*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $output = $output & $file & @CRLF WEnd clipput($output) FileClose($search)Paste the content of the clipboard to get the result.CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 (edited) D: ?? why is it getting to this point? Could you provide a list of the files your script finds ?Dim $output = @TempDir & @CRLF FileChangeDir(@TempDir) $search = FileFindFirstFile("*.*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $output = $output & $file & @CRLF WEnd clipput($output) FileClose($search)Paste the content of the clipboard to get the result.CheersKurt<{POST_SNAPBACK}>I pasted some random files and folders in the temp directory for testing.D:\TEMP...Bookoo DriversCleanup.batclientmaint.exeDefault.rdpExchangePerflog_8484fa312044be6bcfcccd43.datMAS Expense Report Template1.xlsMy Chat LogsNew Text document.txtNew Text document.vbsnodomainui.exeprinters.cmd.txtWindowsServer2003-KB811233-x86-enu.exe~DF9BFD.tmp~DF9C05.tmp~DFAF28.tmp~WRD0003.doc~WRF0001.tmpI ran it on a second machine to see if the same thing happened and it did;C:\WINDOWS\Temp...cinfo.xmlDellJavaMessengerMSNmsnms.icomsnsusii.exexfp.xml Edited June 9, 2005 by snooz Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 I pasted some random files and folders in the temp directory for testing.D:\TEMP...well, then it's clear. It tries to delete "D:\TEMP\.." which is "D:". So, filter out "." and ".." in your script, to get rid of the error message.; After FileFindNext if $file = "." OR $file = ".." then continueloop endifCheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 (edited) well, then it's clear. It tries to delete "D:\TEMP\.." which is "D:". So, filter out "." and ".." in your script, to get rid of the error message.; After FileFindNext if $file = "." OR $file = ".." then continueloop endifCheersKurt<{POST_SNAPBACK}>That was it worked like a charm your the man! Time to test it in SMS I want to post the working code for everybody else;FileChangeDir(@TempDir)$search = FileFindFirstFile("*.*") ; Check if the search was successfulIf $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") ExitEndIfWhile 1 $file = FileFindNextFile($search) if $file = "." OR $file = ".." then continueloop endifIf @error Then ExitLoop if StringInStr(FileGetAttrib($file),"d") then DirRemove($file, 1);- !! Recursive delete !! else FileDelete($file) endifWEnd; Close the search handleFileClose($search)RunWait(@ComSpec & ' /c defrag c:')RunWait(@ComSpec & ' /c defrag d:')RunWait(@ComSpec & ' /c defrag e:')RunWait(@ComSpec & ' /c defrag f:') Edited June 9, 2005 by snooz Link to comment Share on other sites More sharing options...
snooz Posted June 9, 2005 Author Share Posted June 9, 2005 That did it SMS reported as a success Link to comment Share on other sites More sharing options...
/dev/null Posted June 9, 2005 Share Posted June 9, 2005 That did it SMS reported as a success <{POST_SNAPBACK}>well done CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
blindwig Posted June 9, 2005 Share Posted June 9, 2005 In general: really good point!! Allways try to think the "unthinkable"However, in my sample code DirRemove() will not delete the current directory "." or the parent directory ".." as we changed the current directory with FileChangeDir(), which prevents it from beeing deleted. DirRemove(".") will return 0 (== Error).CheersKurt<{POST_SNAPBACK}>weather you delete it or not, you're still going to get stuck in an endless loop.C:\temp = C:\temp\..\temp = C:\temp\..\temp\..\temp etcandC:\temp = C:\temp\. = C:\temp\.\. = C:\temp\.\.\. etcso yes, anytime you are recursing through directory structure, you need to exclude '.' and '..' My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions 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