File.Exists() returns true even if it the file name is in lower case

Hi Guys,

I am using File.exists() method to check if the file exists but it returns true even if its in lower case or Upper case but I want to check if file exists with exact name and want it to be case sensitive.

I tried with directory.get files but its case insensitive too.

for example:
New_Test.pdf → should return true - > returns true
NEW_Test.pdf → should return false- > but returns true
new_Test.pdf → should return false- > but returns true
nEW_Test.pdf → should return false- > but returns true


Hi @Akshay_Suryawanshi

In UiPath, the File.Exists() method and the Directory.GetFiles() method are case-insensitive by default. If you want to perform a case-sensitive file existence check, you can use the Directory.EnumerateFiles() method along with a custom compare.

directoryPath = "C:\YourDirectoryPath"
fileName = "New_Test.pdf"
fileNames = Directory.EnumerateFiles(directoryPath, fileName, SearchOption.TopDirectoryOnly)
For Each file In fileNames
    If String.Equals(file, fileName, StringComparison.Ordinal) Then
    End If

By using the StringComparison.Ordinal option in the String.Equals() method, you can ensure that the file existence check is case-sensitive.

Hope this helps,
Best Regards.


Can you try the following condition?



Hi @Yoichi

Thanks, Its working for single file, can you share the same for multiple files.

Example : New_test1.pdf, New_test2.pdf, New_test3.pdf

Directory.GetFiles(FilePath,FileName+ “*.pdf”).Count=CInt(CurrentRow(“# of Expected PDFs”).ToString)

this method works but I want to have the upper logic for this aswell
I want it case sensitive as well


How about the following?

arrFiles = {"New_test1.pdf", "New_test2.pdf", "New_test3.pdf"}


 arrFiles.Except(System.IO.Directory.GetFiles("path").Select(Function(f) System.IO.Path.GetFileName(f))).Count=0

This means all 3 files exists in the folder.


1 Like

Hi @Yoichi

It returns true for case insensitive as well.



In my environment it works even if case insensitive.
Can you check the following simple sample? (2.8 KB)


I think I got why I am getting wrong output Directory.GetFiles(in_Config(“FolderPath”).ToString,CurrentRow(“Name”).ToString + “*.pdf”).Except(System.IO.Directory.GetFiles(in_Config(“FolderPath”).ToString).Select(Function(f) System.IO.Path.GetFileName(f))).Count=0

Directory.GetFiles(in_Config(“FolderPath”).ToString,CurrentRow(“Name”).ToString + “*.pdf”) return the whole path not just the file name

any alternate for it?

Please add .Select(Function(f) System.IO.Path.GetFileName(f)) after pdf")
But what do you want compare with? Your expression seems to compare with same files.


It checks if the file name mentioned in data table are available in the shared folder. with 1,2,3 string as wild card