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

I’m late to the party, but if you are using a UiPath Modern Project, you should now have access to Directory.GetFiles(String, String, EnumerationOptions) which would be used like so:

ASSIGN arrFilePaths = System.IO.Directory.GetFiles(
  in_Config("FolderPath").ToString,
  CurrentRow("Name").ToString + "*.pdf",
  New System.IO.EnumerationOptions With { .MatchCasing = System.IO.MatchCasing.CaseSensitive }
)
ASSIGN boolFileExists = System.IO.Directory.GetFiles(
  in_Config("FolderPath").ToString,
  CurrentRow("Name").ToString + "*.pdf",
  New System.IO.EnumerationOptions With { .MatchCasing = System.IO.MatchCasing.CaseSensitive }
).Count() > 0

If you are still working with older UiPath Legacy projects (or the above method isn’t available), you can take the more manual approach.

  1. Build your file name pattern
ASSIGN strFileNamePattern = CurrentRow("Name").ToString + "*.pdf"
  1. Use that pattern to search the directory, then filter the returned results using the case-sensitive-by-default Like operator and check if a file exists or build an array of file names or paths.
ASSIGN boolFileExists = System.IO.Directory.
  GetFiles(in_Config("FolderPath").ToString, strFileNamePattern).
  Any(Function (strFilePath) System.IO.Path.GetFileName(strFilePath) Like strFileNamePattern)

or

ASSIGN arrFilePaths = System.IO.Directory.
  GetFiles(in_Config("FolderPath").ToString, strFileNamePattern).
  Where(Function (strFilePath) System.IO.Path.GetFileName(strFilePath) Like strFileNamePattern).
  ToArray

or

ASSIGN arrFileNames = System.IO.Directory.
  GetFiles(in_Config("FolderPath").ToString, strFileNamePattern).
  Select(Function (strFilePath) System.IO.Path.GetFileName(strFilePath)).
  Where(Function (strFileName) strFileName Like strFileNamePattern).
  ToArray

Notes:

  • Some activities in the new versions of Studio broke support for multi-line expressions as I’ve used above. If you encounter this, just delete the spacing between the lines (i.e. "Directory.\n GetFiles" becomes "Directory.GetFiles" and so on.).
  • The above code samples are for Visual Basic. However, the C# syntax is pretty similar but you will need to use Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString instead of the VB Like operator.
  • The class namespaces (such as System.IO.) can be removed from the above samples as they were included for clarity and ease of copy-paste. Each namespace needs to be added into the Imports list (next to Variables and Arguments) if not already present.
  • You could insert strFileNamePattern inline instead of using a separate variable, but I think that’s noisy and will keep your “WTFs per minute” score lower later on when you come back to that code.
  • If your pattern should match "New_Test1.pdf" but not "New Test.pdf", use "?*.pdf" in strFileNamePattern instead as this will ensure at least one extra character is present.
Supporting "[" and "#" characters

In the unlikely case that the literal part of your strFileNamePattern (the CurrentRow("Name").ToString part) includes a # or [ character you will get unexpected results as they are used as-is in Directory.GetFiles and the Like operator treats them as special characters.

To overcome this, you will need to use two patterns - one for Directory.GetFiles and one for the Like operator:

ASSIGN strFileNamePattern = CurrentRow("Name").ToString + "*.pdf"
ASSIGN strFileNameLikePattern = System.Text.RegularExpressions.Regex.Replace(CurrentRow("Name").ToString, "[\[#]", "[$0]") + "*.pdf"

Then add the relevant variables into their slots in the above samples like so:

ASSIGN arrFileNames = System.IO.Directory.
  GetFiles(in_Config("FolderPath").ToString, strFileNamePattern).
  Select(Function (strFilePath) System.IO.Path.GetFileName(strFilePath)).
  Where(Function (strFileName) strFileName Like strFileNameLikePattern).
  ToArray