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.
- Build your file name pattern
ASSIGN strFileNamePattern = CurrentRow("Name").ToString + "*.pdf"
- 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