Hey I have a code where I want to find .jpg file in different folders so my code looks like this :
file = Dir(folder & "\*.jpg")
But it turns out there are also .png file in these folders, is there an easy and simple way to make it so that the Dir() also find png images ? Maybe using regular expression somehow or even an even more simple way I didn't think of ? I know there are a lots of way, but I'd like it if I don't have to change all my code... Thanks for anyone answering :)
CodePudding user response:
Please, try the next way:
Dim file as string, folder as string, arrExt
folder = "your folder path"
file = Dir(folder & "\*.*")
Do While file <> ""
arrExt = split(file, ".")
If UCase(arrExt(Ubound(arrExt))) = "JPG" Or _
UCase(arrExt(Ubound(arrExt))) = = "PNG" Then
' do whatever you need
End If
file = Dir
Loop
CodePudding user response:
Use Dir(folder & "\*.*") to list all files and check by hand if it is a file you are interested in, eg with a function like this:
Function isImage(fileName As String) As Boolean
Dim ext As String, p As Long
p = InStrRev(fileName, ".")
If p = 0 Then Exit Function
ext = LCase(Mid(fileName, p 1))
isImage = (ext = "jpg" Or ext = "jpeg" Or ext = "png")
End Function
