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?
Lines in red is where the error occurs if a single file is left in the directory.
Regards,
Herman
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