Check if file exists with filesystemobject

Viewed 48

I have created a Do While Loop to loop through files with .jpg extensions so as to spot on the first one using such a code

fn = Dir(sPath & "*.jpg")
Do While fn <> ""
    sFile = sPath & fn
    Exit Do
    fn = Dir
Loop

And later in the code, I have put a line that checks if the file exists or not, so I used FileSystemObject like that

Set fso = CreateObject("Scripting.FileSystemObject")
Debug.Print fso.FileExists(sFile)

But I got False as a result in the immediate window, although I expect to return True The file path looks like that C:\Users\Future\Desktop\Screenshot_2022-06-08-21-25-36-252_com.unicostudio.jpg

enter image description here

1 Answers

After the loop part, I have used a UDF that replaces the English numbers to Arabic

Public Function ReplaceEnglishNumbers(strInput As String) As String
    Dim numberArray, i As Long
    numberArray = Array("0", ChrW(&H660), "1", ChrW(&H661), "2", ChrW(&H662), "3", ChrW(&H663), "4", ChrW(&H664), "5", ChrW(&H665), "6", ChrW(&H666), "7", ChrW(&H667), "8", ChrW(&H668), "9", ChrW(&H669))
    ReplaceEnglishNumbers = strInput
    For i = 0 To 18 Step 2
        ReplaceEnglishNumbers = Replace(ReplaceEnglishNumbers, numberArray(i), numberArray(i + 1))
    Next i
End Function

I used the line after the loop to replace the English numbers to Arabic numbers

sFile = ReplaceEnglishNumbers(sFile)
Debug.Print fso.FileExists(sFile), sFile

At last, it returns True, so the problem was with the numbers in the filename. It seems the numbers are read differently inside the same code.

Related