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

Thanks

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
Next

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.

Hi,

Can you try the following condition?

System.IO.Path.GetFileName(System.IO.Directory.GetFiles("yourPath","New_Test.pdf").First())="New_Test.pdf"

Regards,

Hi @Yoichi

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

example:
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

HI,

How about the following?

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

Then

 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.

Regards,

1 Like

Hi @Yoichi

It returns true for case insensitive as well.

Thanks

Hi,

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

Sample20230707-4L.zip (2.8 KB)

Regards,

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.

Regards,

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