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?
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
The FileSystem object may fall over at the first sign of trouble if you use wilcards.

But, if you write your own loop (to enumerate all files in the folder/subfolders) it should work perfectly.
Answered 02/08/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
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.
Answered 02/08/2005 by: kkaminsk
Ninth Degree Black Belt

Please log in to comment
0
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
Answered 02/08/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
2nd line of the script deletes files with .tmp extension.
How would one search for files that have ~ (tildas) anywhere in the name of the files as such files are the main files requiring deletion.
Answered 05/23/2005 by: oofemioo
Blue Belt

Please log in to comment
0
It should simply be a case of changing the second line?

DeleteFiles "~", "C:\Windows\Temp", True

I haven't tried it, but it should work.
Answered 05/24/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
Answer this question or Comment on this question for clarity