Deleting temporary files
I intend writing a script to delete temporary files on the system.
I know the FileSystemObject in VBScript/WSH can do this but I need to add some intelligence whereby if the file can't be deleted the script should skip the file and move to the next one.
One problem I do envisage is the fact the FileSystemObject isn't that intelligent to accomplish the task.
I could have put my trust in WMI using e.g. available from Technet and ScriptCentre but files starting with ~*** may fail when one tries to delete them especially when the original file is open.
Does anyone have any suggestions/ideas or knows of any tool in the market which does the aforementioned?
I know the FileSystemObject in VBScript/WSH can do this but I need to add some intelligence whereby if the file can't be deleted the script should skip the file and move to the next one.
One problem I do envisage is the fact the FileSystemObject isn't that intelligent to accomplish the task.
I could have put my trust in WMI using e.g. available from Technet and ScriptCentre but files starting with ~*** may fail when one tries to delete them especially when the original file is open.
Does anyone have any suggestions/ideas or knows of any tool in the market which does the aforementioned?
0 Comments
[ + ] Show comments
Answers (5)
Please log in to answer
Posted by:
WiseUser
19 years ago
Posted by:
kkaminsk
19 years ago
I think that is something to definately keep in mind when trying to do wildcard type filesystem actions in VBScript. It takes more code than you think to get things done right. I am working on a script to enumerate files, check them with a destination, copy them if version is newer and then have all the error handling for everything that can go wrong. Let's just say it went from simple script to a significant undertaking.
Posted by:
WiseUser
19 years ago
Here's a quick untested modification of a script I posted here a few days ago. Might be of use?
Set oFso = CreateObject("Scripting.FileSystemObject")
DeleteFiles ".tmp", "C:\Windows\Temp", True
Sub DeleteFiles(sSearch, sFolder, bRecurse)
Set oFolder = oFso.GetFolder(sFolder)
Set cFiles = oFolder.Files
For Each oFile in cFiles
If Instr(LCase(oFile.Name),LCase(sSearch)) <> 0 Then
oFso.DeleteFile oFile.Path, True
End If
Next
Set cSubFolders = oFolder.SubFolders
If bRecurse Then
For Each oSub in cSubFolders
DeleteFiles sSearch, oSub.Path, bRecurse
Next
End If
End Sub
Set oFso = Nothing
DeleteFiles ".tmp", "C:\Windows\Temp", True
Sub DeleteFiles(sSearch, sFolder, bRecurse)
Set oFolder = oFso.GetFolder(sFolder)
Set cFiles = oFolder.Files
For Each oFile in cFiles
If Instr(LCase(oFile.Name),LCase(sSearch)) <> 0 Then
oFso.DeleteFile oFile.Path, True
End If
Next
Set cSubFolders = oFolder.SubFolders
If bRecurse Then
For Each oSub in cSubFolders
DeleteFiles sSearch, oSub.Path, bRecurse
Next
End If
End Sub
Set oFso = Nothing
Posted by:
oofemioo
19 years ago
Posted by:
WiseUser
19 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.