Solved

VBA - Receiving error when trying to kill a file

Posted on 2016-08-02
10
45 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
  • 5
  • 5
10 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You probably need the name only:

    Kill collection(i).Name

/gustav
0
 

Author Comment

by:rrudolph
Comment Utility
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 49

Expert Comment

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

    collection(i).Delete

/gustav
0
 

Author Comment

by:rrudolph
Comment Utility
This generates a different error...Permission denied
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:rrudolph
Comment Utility
I do have delete rights, and I can delete it manually
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
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
Comment Utility
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 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now