/build/static/layout/Breadcrumb_cap_w.png

Deleting files in c:\temp

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

Answers (5)

Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
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
Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
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. :-)
Posted by: Naffcat 19 years ago
Senior Purple Belt
0
Many thanks - will this handle open files ok?
Posted by: Naffcat 19 years ago
Senior Purple Belt
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.
Posted by: Byoung4now 19 years ago
Senior Yellow Belt
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
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ