Does anyone have a script which could delete all files in c:\temp and skip over in that are in use?
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
Won't delete sub-folders but should do the files...

On Error Resume Next

set oFso = CreateObject("Scripting.FileSystemObject")

oFso.DeleteFile "C:\Temp\*.*", True

Set oFso = Nothing
Answered 11/05/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
Just found a better one on my D-drive.... should take care of the files in subfolders too (but not the folders themselves).


Set oFso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "", "C:\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

Notice that you can search for patterns in the file names to be more specific about what you want to delete. :-)
Answered 11/05/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
Many thanks - will this handle open files ok?
Answered 11/07/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
I would like the equivalent of the below...

c:
cd %TEMP%
del *.* /q

but in VB ... it seems it's not obvious to me how to handle in use files.
Answered 11/09/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
This will put the files to delete in an array. If a file is in use it will skip it.

If FSO.FolderExists ("c:\temp") Then
path = "c:\temp"
arfiles = Array()
SelectFiles path,arFiles, True
for n = 0 To ubound(arFiles)
on error Resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number = 0 then
'wscript.echo "Unable to delete: " & arFiles(n).path
Else
end If
on error goto 0
Next
end If

'recursive delete
Sub SelectFiles(sPath, arFilesToKill, bIncludeSubFolders)
'select files to delete and add to array...
Set folder = fso.getfolder(sPath)
set files = folder.files
for Each file in files
count = ubound(arFilesToKill) + 1
ReDim preserve arFilesToKill(count)
Set arFilesToKill(count) = file
Next
if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,arFilesToKill,True
Next
end If
end Sub
Answered 11/18/2005 by: Byoung4now
Senior Yellow Belt

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