Deleting files with FSO with certain creteria

Hi Experts,

Is it possible to delete files using the File System Object that have a certain criteria. Either files created earlier than 8/25/2013 or files that start off with 08252013*.zip.

Any help will be appreciated.
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Not with a single objFSO.Delete() command.  What you would need to do is actually iterate through all the files in the folder involved, and do the checking for a match yourself in additional code.  Then when a match is found you can delete it.

Let me know if you need help with that part.

(Are all the files to be checked in a single folder?)

~bp
0
APD TorontoSoftware DeveloperAuthor Commented:
Can you elaborate?
0
Bill PrewCommented:
Okay, here's a basic example that will look at the files in the specified folder (change for your needs) and then delete the matching files.  Right now I have the actual Delete of the file commented out, so it will display the name of the file(s) it would delete but not actually delete them, for testing.  If it looks good then uncomment the Delete line.

' Define folder to search for files in
strBaseDir = "C:\EE\EE28223948\Files"

' Define criteria used to select files for deletion
datMatchDate = #8/25/2013#
strMatchLeft = "08252013"
strMatchRight = ".zip"

' Create filesystem object, and access the folder to process
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Look at each file in the folder
For Each objFile In objFolder.Files
   ' See if it meets the criteria for deletion
   If IsMatch(objFile) Then
      ' Display file being deleted, and delete it
      Wscript.Echo "DELETE File:[" & objFile.Path & "]"
      ' objFile.Delete
   End If
Next

' Implement selection criteria for deletion
Function IsMatch(objCheck)
   ' If the left and right parts of the name match then flag to delete
   If LCase(Left(objCheck.Name, Len(strMatchLeft))) = LCase(strMatchLeft) And _
      LCase(Right(objCheck.Name, Len(strMatchRight))) = LCase(strMatchRight)Then
      IsMatch = True
      Exit Function
   End If

   ' If file was created before check date then flag to delete
   If objCheck.DateCreated < datMatchDate Then
      IsMatch = True
      Exit Function
   End If

   ' Otherwise don't delete the file
   IsMatch = False
End Function

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

APD TorontoSoftware DeveloperAuthor Commented:
This is my code

    Dim fso, fsoFolder, objfile
    
    Set fso = CreateObject("Scripting.FileSystemOject")
    Set fsoFolder = fso.GetFolder(mp3Path)
    
    Dim i As Integer
    
    For i = 0 To 1 ' should be 1to10 'Delete from yesterday to 10days
    
        For Each objfile In fsoFolder.Files
            
            If objfile.DateCreated <= (Date - i) Then
                
                objfile.Delete
                
            End If
            
        Next
    
    Next i
    
    Set fso = Nothing
    Set fsoFolder = Nothing

Open in new window


but I get
ActiveX component can't create object

Open in new window


at:
Set fso = CreateObject("Scripting.FileSystemOject")

Open in new window

0
Bill PrewCommented:
How are you running it?

~bp
0
APD TorontoSoftware DeveloperAuthor Commented:
vb6
0
Bill PrewCommented:
Ah, what I posted was a VBS script, not VB6.  It would normally be run from the command prompt directly like:

cscript ee28223948.vbs

~bp
0
APD TorontoSoftware DeveloperAuthor Commented:
but, shouldn't it run under both, vbs and vb6?
0
Bill PrewCommented:
It can be adjusted to run under VB6, I'm not an expert there but I think it's in the way you create the object.  See if this gets you close:

Option Explicit

Dim fso As New FileSystemObject
Dim fsoFolder As Folder
Dim objFile As File

Set fsoFolder = fso.GetFolder(mp3Path)

Dim i As Integer

For i = 0 To 1 ' should be 1to10 'Delete from yesterday to 10days

    For Each objfile In fsoFolder.Files
        
        If objfile.DateCreated <= (Date - i) Then
            
            objfile.Delete
            
        End If
        
    Next

Next i

Set fso = Nothing
Set fsoFolder = Nothing

Open in new window

~bp
0
APD TorontoSoftware DeveloperAuthor Commented:
Under vb6 worked, but now when I made it into vbs I get the

ActiveX component can't create object

my code now is

'    Dim fso As New FileSystemObject '***comment OUT for vbs
'    Dim fsoFolder As Folder '***comment OUT for vbs
'    Dim objfile As File '***comment OUT for vbs
    
    Set fso = CreateObject("Scripting.FileSystemOject") '***comment IN for vbs
    Set fsoFolder = fso.GetFolder(mp3Path)
    
'    Dim i As Integer
    
    For i = 1 To 10 'Delete from yesterday to 10days
    
        For Each objfile In fsoFolder.Files
            
            If objfile.DateCreated <= (Date - i) Then
                
                objfile.Delete
                
            End If
            
        Next
    
    Next 'i
    
    Set fso = Nothing
    Set fsoFolder = Nothing

Open in new window

0
Bill PrewCommented:
That doesn't sound like a VBS script error, are you running it from CSCRIPT?

~bp
0
Bill PrewCommented:
Also, I don't see mp3path set in that script?

~bp
0
APD TorontoSoftware DeveloperAuthor Commented:
cscript?

I just pasted it in notepads, saved it .vbs, and double-clicked it to run./
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.