?
Solved

VBA - Receiving error when trying to kill a file

Posted on 2016-08-02
10
Medium Priority
?
74 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 51

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 51

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

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

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 51

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 51

Accepted Solution

by:
Gustav Brock earned 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

752 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