Get file names from a directory where pattern is having spaces in between

Hi All,

I have files as follows in a directory.
1-abc.pdf
17 - xyz.pdf
18 -fdf.pdf
1 - fff.pdf
1 -tyty.pdf

and i need to get the files that starts with “1-” or "1 - "or “1 -”
When i am using directory.getfiles(Path,“1*.pdf”) i am getting all the files including 17 and 18.
I just need the files that start with 1 followed by - or file that start with 1 followed by space and -.
So in this case i just need the files
1-abc.pdf
1 - fff.pdf
1 -tyty.pdf
Please help

Hi @rahul.lakshmanan

Inside the loop add an if condition it does startswith “1-” or “1 -” or"1 - "

I was looking for something that will filter the same in the directory.getfiles section instead of using an if condition to filter

Hi,

The following might help you.

arrFiles = System.IO.Directory.GetFiles(yourPath,"*.pdf")

arrFilteredFiles = arrFiles.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(x,"^.*\\1\s?\-.*$")).ToArray

Regards,

Try the below code
Directory.GetFiles(<path>,"1*.pdf").Where(Function(f) f.StartsWith("1 ") OR f.StartsWith("1 -") OR f.StartsWith("1 - "))

@Yoichi and @aanandsanraj the value of 1 comes from a variable… How can i add the variable in the code instead of 1

Hi,

Can you try the following if your variable (yourVar) doesn’t contains special character in regex.

arrFilteredFiles = arrFiles.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(x,"^.*\\"+yourVar+"\s?\-.*$")).ToArray

Regards,

this does not work…it returns no values

@Yoichi and @aanandsanraj it is not working because the folder name that is used by the files also has 1-.and hence it is returning all the files
Example:
C:\Test*1-Client Documents*\1-abc.pdf
C:\Test*1-Client Documents*\17 - xyz.pdf
C:\Test*1-Client Documents*\18 -fdf.pdf
C:\Test\1-Client Documents\1 - fff.pdf
C:\Test\1-Client Documents\1 -tyty.pdf
Any idea how we can solve this?

@Palaniyappan can you pls help?

1 Like

Hi
Hope this expression would help you resolve this

arrFiles = System.IO.Directory.GetFiles(yourfolderpath,“*.pdf”)

Then another assign activity like this

arr_filepath = arrFiles.Where(Function(x) System.Text.RegularExpressions.Regex.IsMatch(x,”(1)\s+|(1)-“).ToArray()

Cheers @rahul.lakshmanan

@Palaniyappan Let me try and get back to you. can we put a variable instead of 1?

Yah we can
But if we are sure it’s going to be 1 always then it can with 1 directly
Or we can replace with a variable as mentioned earlier

Cheers @rahul.lakshmanan

@Palaniyappan we are getting the values from an excel cell, so it willl keep changing.

Fine
@rahul.lakshmanan

@Palaniyappan that code did not work, but the following worked
string variable item =“1”
strItem=item+“*.pdf"
list = Directory.GetFiles(in_DocumentPath,strItem).Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(x,“\”+item+"(?=\D)(?:[^\\n.]
.)
?[^.\\n]+.pdf$”)).ToList

3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.