Jump to content

Help with clearing out %TEMP%


Recommended Posts

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 by snooz
Link to comment
Share on other sites

Link to comment
Share on other sites

unable to locate anything for clearing out the %TEMP% directory.  I tried using a

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)

Cheers

Kurt

__________________________________________________________(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

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)

Cheers

Kurt

<{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 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)

RunWait(@ComSpec & ' /c defrag c:')

RunWait(@ComSpec & ' /c defrag d:')

RunWait(@ComSpec & ' /c defrag e:')

RunWait(@ComSpec & ' /c defrag f:')

Edited by snooz
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Cheers

Kurt

__________________________________________________________(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

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 by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

You need to exclude the '.' and '..' directories, or bad things will happen

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).

Cheers

Kurt

__________________________________________________________(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

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).

Cheers

Kurt

<{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

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.

Cheers

Kurt

__________________________________________________________(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

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.

Cheers

Kurt

<{POST_SNAPBACK}>

I pasted some random files and folders in the temp directory for testing.

D:\TEMP

.

..

Bookoo Drivers

Cleanup.bat

clientmaint.exe

Default.rdp

ExchangePerflog_8484fa312044be6bcfcccd43.dat

MAS Expense Report Template1.xls

My Chat Logs

New Text document.txt

New Text document.vbs

nodomainui.exe

printers.cmd.txt

WindowsServer2003-KB811233-x86-enu.exe

~DF9BFD.tmp

~DF9C05.tmp

~DFAF28.tmp

~WRD0003.doc

~WRF0001.tmp

I ran it on a second machine to see if the same thing happened and it did;

C:\WINDOWS\Temp

.

..

cinfo.xml

Dell

Java

Messenger

MSN

msnms.ico

msnsusii.exe

xfp.xml

Edited by snooz
Link to comment
Share on other sites

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
endif

Cheers

Kurt

__________________________________________________________(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

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
endif

Cheers

Kurt

<{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 successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

if $file = "." OR $file = ".." then

continueloop

endif

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)

RunWait(@ComSpec & ' /c defrag c:')

RunWait(@ComSpec & ' /c defrag d:')

RunWait(@ComSpec & ' /c defrag e:')

RunWait(@ComSpec & ' /c defrag f:')

Edited by snooz
Link to comment
Share on other sites

That did it SMS reported as a success :(

<{POST_SNAPBACK}>

well done :(

Cheers

Kurt

__________________________________________________________(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

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).

Cheers

Kurt

<{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 etc

and

C:\temp = C:\temp\. = C:\temp\.\. = C:\temp\.\.\. etc

so yes, anytime you are recursing through directory structure, you need to exclude '.' and '..'

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...