Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete files & folders

Status
Not open for further replies.

biglebowski

Technical User
Jan 29, 2004
3,117
GB
I have a script to delete all WAV files older than 365 days in a specific folder and subfolders. The file structure of the folder is

E:\Audio\Date\Machine\Channel\xxxxx.WAV

The script runs without errors but does not delete anything, am I missing something simple here?

Also is it possible to delete all the folders as well?
Option Explicit
Dim arExtensions

arExtensions = Array("wav")

DelByAgeAndExtension "E:\Audio","365","c",arExtensions

Function DelByAgeAndExtension(strFolder,strDaysOld, strCreateOrMod,
arExtensions)
Dim fso, wshShell, objFolder, colFiles, colSubFolders, objFile,
subFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("Wscript.Shell")
Set objFolder = fso.GetFolder(strFolder)
Set colFiles = objFolder.Files
Set colSubFolders = objFolder.SubFolders

'delete files matching extension in parent folder
On Error Resume Next
For Each objFile In colFiles
DelFileByExtension objFile, strDaysOld,arExtensions, strCreateOrMod
Next

'delete files matching extensions in each sub folder
For Each subFolder In colSubFolders
Set colFiles = subFolder.Files
For Each objFile In colFiles
DelFileByExtension objFile,strDaysOld,arExtensions, strCreateOrMod
Next
Next
End Function

Function GetFileCreatedAgeInDays(objFile)
Dim fso, objCheckFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set objCheckFile = fso.GetFile(objFile)
GetFileCreatedAgeInDays = (Now - objCheckFile.DateCreated)
End Function

Function GetFileModifiedAgeInDays(objFile)
Dim fso, objCheckFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.GetFile(objFile)
GetFileModifiedAgeInDays = (Now - objFile.DateLastModified)
End Function

Sub DelFileByExtension(objFile,strDaysOld,arExtensions,strCreateOrMod)
Dim i
For i = 0 To UBound(arExtensions)
If LCase(Right(objFile.ShortName,3)) = LCase(arExtensions(i)) Then
'if strDaysOld set to -1 then delete all files
'matching extension regardless of age
If strDaysOld = "-1" Then
objFile.Delete
Else
If strCreateOrMod = "C" Then
If GetFileCreatedAgeInDays(objFile) >= Int(strDaysOld) Then
objFile.Delete
End If
Else 'deletes based on modified date if strDaysOld is anything _
other than "C"
If GetFileModifiedAgeInDays(objFile) >= Int(strDaysOld) Then
objFile.Delete
End If
End If
End If
End If
Next
End Sub


When I was born I was so suprised I didn't talk for 18 months
 
I think this may have something to do with folder depth. If I move the wav file into the machine folder it is deleted but anywhere beyond that it is not deleted.

Is there a limit on the number of sub folders that can be scanned by a script? (or this script)

Thanks

When I was born I was so suprised I didn't talk for 18 months
 
>If strCreateOrMod = "C" Then
Better uniformize the case. In this concrete case, you pass on "c" (lower case) then it would fail to match!
[tt]If ucase(strCreateOrMod) = "C" Then[/tt]
 
I changed the case but it still doesn't delete. If the strdaysold is not C wouldn't the rule then delete based on modified date?

If strCreateOrMod = "C" Then
If GetFileCreatedAgeInDays(objFile) >= Int(strDaysOld) Then
objFile.Delete
End If
Else
If GetFileModifiedAgeInDays(objFile) >= Int(strDaysOld)Then
objFile.Delete




When I was born I was so suprised I didn't talk for 18 months
 
OK it's definately a path issue if I change the strFolder to the path of one of the wav files it then deletes ok

DelByAgeAndExtension "E:\Audio\Date\Machine\Channel\Time\Audio","-1","C",arExtensions

But when it's set to

DelByAgeAndExtension "E:\Audio","-1","C",arExtensions

Nothing gets deleted???

When I was born I was so suprised I didn't talk for 18 months
 
>DelByAgeAndExtension "C:\Gds", "3", "c", arExtensions

>Sub DelFileByExtension(objFile, strDaysOld, arExtensions, strCreateOrMod)

The arExtensions and strCreateOrMod parameters appear reversed in your Function call try;

DelByAgeAndExtension "C:\Gds", "3", arExtensions, "C
 
If it is to delete to any depth of subfolders, it is this.
[tt]
'delete files matching extensions in each sub folder
For Each subFolder In colSubFolders
[red]'[/red]Set colFiles = subFolder.Files
[red]'[/red]For Each objFile In colFiles
[red]'[/red]DelFileByExtension objFile,strDaysOld,arExtensions, strCreateOrMod
[red]'[/red]Next
[blue]DelByAgeAndExtension subFolder, strDaysOld, strCreateOrMod, arExtensions[/blue]
Next
End Function
[/tt]
side-notes:
[a] In any case, naming of arExtensions global and then use the same name in each function/sub argument risks to confuse yourself some later days, although it is in principle no problem.
Also, as pointed out, comparision for "C" should better be made relaxing the case restriction to upper case, that is how the other comparison seems to be conceived (for file extension).
[c] The extension filtering seems unnecessarily restrictive. It can easily be made more general using
[tt]fso.getExtensionName(path)[/tt]
method. But you do have to overwhelm by all the details at the moment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top