Doonline
(Doonlinejob23)
February 23, 2022, 6:02am
1
Hii ,
I have a folder path
C:\Myfolder\Datafolder
Inside Datafolder i have multiple Excel files
But i have only a keyword to find that file
The keyword is “Y894Z”
and the file name is “Y894Z-MyK.xlsx”
I do i search this file and read it.
Please help ASAP
get all the file names using directory.getfiles. then within a loop put an if condition and check the keyword using regex.
Doonline:
C:\Myfolder\Datafolder
HI @Doonline
You can use directory.get files like below
String arrayVar AllFiles = Directory.GetFiles("C:\Myfolder\Datafolder",".xlsx")
Then you can apply a foreach loop on this array and if item.Contains("yourKeyword’) thats your file
Thanks,
Prankur
Doonline
(Doonlinejob23)
February 23, 2022, 6:16am
5
can u provide a regex expression
Hi @Doonline ,
If the File with the Keyword is for sure present, We can use the below Expression to get the File based on the Keyword.
Directory.GetFiles("YourFolderPath","*Y894Z*.xlsx").FirstOrDefault
The above expression would get you the Excel file path containing the Keyword in it’s name, if not present it would output null.
Let us know if this doesn’t work.
1 Like
Doonline
(Doonlinejob23)
February 23, 2022, 6:19am
7
Sometimes it can be xlsx and can be xlsm.So i cant provide the extension
Gokul001
(Gokul Balaji)
February 23, 2022, 6:20am
8
@Doonline
Steps will be like
Use For each file in folder pass your folder path give your extension there or you can give “. ” to get all type of extensios
If (Split(CurrentFile.Name,“-”)(0).Contains(“Y894Z”))
Where CurrentFile.Name will get you the Name.xlsx of each file present in the provided path
Regards
Gokul
1 Like
@Doonline ,
We can ignore the extension :
Directory.GetFiles("YourFolderPath","*Y894Z*").FirstOrDefault
If only xlsm
or xlsx
are the extensions you are searching for and only one of them is present, then we could use the below Expression :
Directory.GetFiles("YourFolderPath","*Y894Z*.xlsx|*Y894Z*.xlsm").FirstOrDefault
If there are multiple files with the same Keyword present, then we can modify the Expression to collect all the matching file like below and then loop over them and read the files one by one.
Directory.GetFiles("YourFolderPath","*Y894Z*")
1 Like
Doonline
(Doonlinejob23)
February 23, 2022, 6:25am
10
Brother i have just provide u an example like it is separated by “-”.In real time the scenario is different.I only want to search the file name with keyword.After keyword it can be anything.But the keyword will be unique thats for sure
Gokul001
(Gokul Balaji)
February 23, 2022, 6:28am
11
Okay So you will know the whole key word and it will be at the beginning , right ?
Doonline
(Doonlinejob23)
February 23, 2022, 6:29am
12
Keyword is stored in an variable and whem i m mentioning it with * i m getting error
@Doonline ,
What is the Error that you have received?
Could you provide us a Screenshot ?
Gokul001
(Gokul Balaji)
February 23, 2022, 6:34am
15
Gokul001:
Hi @Doonline
You can try like this
Regards
Gokul
So in the above if condition you can pass like this
System.text.RegularExpression.Regex.IsMatch(CurrentFile.Name,“^”+KeywordVAr)
It will return bool value
Regards
Gokul
Doonline
(Doonlinejob23)
February 23, 2022, 6:38am
16
formate is not same it can be “_” or “-” but the keyword is same and unique
Gokul001
(Gokul Balaji)
February 23, 2022, 6:44am
17
Doonline:
“_” or “-”
I have not used this as format in the above condition
This expression will check whether the file name starts with the keyword you given
If your key word can be anywhere in the fileName you can use this expression
System.text.RegularExpression.Regex.IsMatch(CurrentFile.Name,KeywordVAr)
Regards
Gokul
2 Likes
system
(system)
Closed
February 26, 2022, 6:44am
18
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.