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)
Please log in to answer
Posted by:
WiseUser
19 years ago
Posted by:
WiseUser
19 years ago
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. :-)
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
Posted by:
Byoung4now
19 years ago
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
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.
so that the conversation will remain readable.