Solved

VBA - Receiving error when trying to kill a file

Posted on 2016-08-02
10
72 Views
Last Modified: 2016-08-07
I have a function that is supposed to delete any file but the 5 newest files in a folder. All of the files are named:

yyymmddhhss.bak

When I run the code, I receive a Run-time error '75'

In debug.print, the path looks perfect. This is an example: F:\QBackup\20160802105355.BAK

This is the correct file

This is the code:

Function KillOldQB()

Dim fso As Object
Dim fcount As Object
Dim collection As New collection
Dim obj As Variant
Dim i As Long


Set fso = CreateObject("Scripting.FileSystemObject")
'add each file to a collection
For Each fcount In fso.GetFolder("F:\Qbackup\").Files

    collection.Add fcount

Next fcount

'sort the collection descending using the CreatedDate
Set collection = SortCollectionDesc(collection)

'kill items from index 6 onwards
For i = 6 To collection.Count

FiletoKill = collection(i)
Debug.Print i
Debug.Print collection(i)
    Kill (collection(i))
Next i

End Function
0
Comment
Question by:rrudolph
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41739656
You probably need the name only:

    Kill collection(i).Name

/gustav
0
 

Author Comment

by:rrudolph
ID: 41739726
This did not work.

These 2 lines:

Debug.Print collection(i)
Debug.Print collection(1).Name

Show this in the immediate Window

F:\QBackup\20160802105355.BAK
20160802111852.bak
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41739755
Well, yes, you would need the full path.
But then, why not just:

    collection(i).Delete

/gustav
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rrudolph
ID: 41739761
This generates a different error...Permission denied
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41740191
OK. If you are not granted delete rights, or the file is in use, you can't delete it.
So that has to be solved before you can proceed.

/gustav
0
 

Author Comment

by:rrudolph
ID: 41740411
I do have delete rights, and I can delete it manually
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41740423
Perhaps when added to your collection, this puts a lock on the file.

Try this:

For Each fcount In fso.GetFolder("F:\Qbackup\").Files

    If fcount.Name = "SomeFileName"  Then
        ' a test file of yours that can be deleted.
        fcount.Delete
    End If

Next fcount


/gustav
0
 

Author Comment

by:rrudolph
ID: 41746132
I don't understand where to insert this code, and am not sure what "SomeFileName" is. After the all the advice on this, this is what I currently have.

Function KillOldQB()

Dim fso As Object
Dim fcount As Object
Dim collection As New collection
Dim obj As Variant
Dim i As Long


Set fso = CreateObject("Scripting.FileSystemObject")
'add each file to a collection
For Each fcount In fso.GetFolder("F:\Qbackup\").Files


    collection.Add fcount

Next fcount

'sort the collection descending using the CreatedDate
Set collection = SortCollectionDesc(collection)

'kill items from index 6 onwards
For i = 6 To collection.Count

FiletoKill = collection(i)
Debug.Print i
Debug.Print collection(i)
Debug.Print collection(1).Name
    collection(i).Delete
Next i

End Function


This code generates a permission denied.

This is the results of the debug.prints

 6
F:\QBackup\20160802105355.BAK
20160802111852.bak
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41746239
It's the For-Each loop you have:

For Each fcount In fso.GetFolder("F:\Qbackup\").Files

    If fcount.Name = "20160802111852.bak"  Then
        ' a test file of yours that can be deleted.
        fcount.Delete
    End If

Next fcount

It will, of course, only delete (if possible) this single file. If it works, while deleting using the collection doesn't, you cannot use a collection as you do.
If it doesn't, either the process misses rights for deletion or something else is holding the file, and this would have to be solved before you can proceed.

/gustav
0
 

Author Closing Comment

by:rrudolph
ID: 41746564
Thanks for your help. Below is what ultimately worked:

Function KillOldQB()

Dim fso As Object
Dim fcount As Object
Dim collection As New collection
Dim obj As Variant
Dim i As Long
Dim sfile As String
Set fso = CreateObject("Scripting.FileSystemObject")
'add each file to a collection
For Each fcount In fso.GetFolder("\\wayne-dc01\data\Qbackup\").Files


    collection.Add fcount

Next fcount

'sort the collection descending using the CreatedDate

Set collection = SortCollectionDesc(collection)

'kill items from index 6 onwards
For i = 6 To collection.Count
sfile = collection(i)


fso.deletefile collection(i), True


Next i

End Function
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question