Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to delete TEMP files and folder

Status
Not open for further replies.

brichr

MIS
Feb 17, 2003
121
US
I have a script to delete the %TEMP% files and folders, but the path is hard coded. This will not work on all the machines on my network sinse each machine has a different path to temp depending on who is using the machine. How can I substitute this path C:\Documents and Settings\user\Local Settings\Temp to be act more like
%temp% Here is the code I use now.

Set fso = CreateObject("Scripting.FileSystemObject")
Path = "C:\Documents and Settings\user\Local Settings\Temp"
Set oFolder = fso.GetFolder(Path)
For Each oFile In oFolder.files
fso.DeleteFile oFile

Next

For Each oSubFolder In oFolder.SubFolders

Call KillSubFolders (oSubFolder)

Next

MsgBox "Done"

Sub KillSubFolders (SubPath)
fso.DeleteFolder SubPath
End Sub
 
I tried this in two locations. Both time got an error. The error said "Object Required = .WshShell"

The two attempts I made look like this
Path = WshShell.ExpandEnvironmentStrings("%TEMP%")

The other one was

Set oFolder = fso.GetFolder(WshShell.ExpandEnvironmentStrings("%TEMP%"))
 
You have to instantiate the WshShell object prior to use it:
Set WshShell = CreateObject("WScript.Shell")

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you both for yor help. My script works great now. Here is the final product I use to remove the temp files and folders. I chose to use VB over a simple batch file because I wanted to check and make sure certain processes were not running first. That part of the code is left out.

et fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set oFolder = fso.GetFolder(WshShell.ExpandEnvironmentStrings("%TEMP%"))
For Each oFile In oFolder.files
fso.DeleteFile oFile

Next

For Each oSubFolder In oFolder.SubFolders

Call KillSubFolders (oSubFolder)

Next

MsgBox "Done"

Sub KillSubFolders (SubPath)
fso.DeleteFolder SubPath
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top