Vba does not want to Kill the Last File in the Directory

Hmerman

Board Regular
Joined
Oct 2, 2016
Messages
102
Hello,
I have a code that when it is clicked it finds all the jpg files in a directory and places there Names in an Array then deletes one by one with Kill. Everything went fine until the last file where I get an Error 53 "File not found". I checked and the file is there. So I changed the code that if the File count is one it should Kill it and not use the Array. Again I get the same error.

So I deleted the file manually and took a picture with the onboard camera. But still the same happens. I checked the folder settings and could not find anything untoward.

Can someone please assist or give advice about this behaviour?

Code:
Dim FileName As String
    Dim FileSpec As String
    Dim Directory As String
    Dim imgNumbers As Long
    Dim CompleteImages() As Variant
    
    'set global variable string to empty
    imgP = vbNullString
    NewestImage = vbNullString
    
    'Specify the file type for images if any
    FileSpec = "*.jpg*"
    
    'specify the directory
    Directory = Environ("UserProfile") & "\Pictures\Camera Roll\"
    FileName = Dir(Directory & FileSpec)
    
'count files in directory
If FileName <> "" Then
    Do While FileName <> ""
        imgNumbers = imgNumbers + 1
        Debug.Print FileName
        FileName = Dir()
    Loop
    Else
        If FileName = "" Then
    Exit Sub
        End If
End If
    're-populate empty string
    FileName = Dir(Directory & FileSpec)

  [COLOR=#008000] 'delete file if count is one [/COLOR]
   If imgNumbers = 1 Then
        [COLOR=#ff0000]Kill FileName[/COLOR]
        Else
    
    imgNumbers = imgNumbers - 1
        
    'Make array dynamic
    ReDim CompleteImages(0 To imgNumbers)
    
    'populate array CompleteImages with jpg file strings in Directory
    For imgNumbers = LBound(CompleteImages) To UBound(CompleteImages)
                FileName = Dir()
                CompleteImages(imgNumbers) = FileName
    Next imgNumbers
                 
              [COLOR=#008000]'delete files in the array CompleteImages[/COLOR]
               For imgNumbers = LBound(CompleteImages) To UBound(CompleteImages)
                  [COLOR=#ff0000] Kill Environ("UserProfile") & "\Pictures\Camera Roll\" & CompleteImages(imgNumbers)[/COLOR]
                Next imgNumbers
    
    Erase CompleteImages
    End If

Lines in red is where the error occurs if a single file is left in the directory.

Regards,
Herman
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Update:
I tried setting the Dir function to include Hidden, System and Read-Only files and then Kill, but still the same Error.
Code:
FileName = Dir(Directory & FileSpec, vbHidden + vbSystem + vbReadOnly)

Can someone assist?
 
Upvote 0
What is the filename? If the file created by a mac it could contain an illegal windows character. If it does it will produce that error.
 
Upvote 0
WIN_20180305_21_47_14_Pro.jpg is the file name.
I took the picture/generated the jpg with Windows Camera App that stores it in C:Users\MyUser\Pictures\Camera Roll\ and follows its own file name convention.
 
Upvote 0
You haven't included the path in the single kill:

Code:
Kill Directory & FileName

and your array is one element too large in the other part - use:

Code:
ReDim CompleteImages(1 To imgNumbers)
 
Upvote 0
Thank you. The kill of the single file path works:
Code:
Kill Directory & FileName
If I set the base item of the Array from 1, one file is left in the folder.

I did subtract one from the imgNumbers with

Code:
imgNumbers = imgNumbers - 1
therefore allowing for the base item of the Array to be 0.

It still gives me an error for the Ubound(imgNumbers) filename.
 
Upvote 0
I don't really understand why you need most of this. Is there a reason you can't simply delete all the jpg's in the directory without creating the array?
 
Upvote 0
It started where CompleteImages Array was a global variable that was populated once with a search button and then used across the userform to load pictures into a Image Control, toggle through the Images one by one, place the one the user wants onto a spreadsheet and of course delete all. But because the user took pictures after clicking search the Array wasn't always updated, hence repopulating the Array Locally.
But as you state, it borders on overKill. Delete all can easily be accomplished with
Code:
Dim FileName As String
    Dim FileSpec As String
    Dim Directory As String
    
    'set global variable string to empty
    imgP = vbNullString
    NewestImage = vbNullString
    
    'Specify the file type for images if any
    FileSpec = "*.jpg*"
    
    'specify the directory
    Directory = Environ("UserProfile") & "\Pictures\Camera Roll\"
    FileName = Dir(Directory & FileSpec)
    
'kill files in directory
If FileName <> "" Then
    Do While FileName <> ""
        Debug.Print FileName
        Kill Directory & FileName
    Loop
    Else
        If FileName = "" Then
    Exit Sub
        End If
End If

Don't know why I did not see it sooner. Definitely because I did not see it. Maybe I was caught up using an Array that worked.
This will run on exit so, I think that it is just what I am going to do. Thank you for your input, clear line of thought and wisdom.

Regards,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top