How to search a excel file in a folder and read that file

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.

Hi @Doonline

You can try like this

image

Regards
Gokul

2 Likes

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

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

Sometimes it can be xlsx and can be xlsm.So i cant provide the extension

@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”))
    • True → Read range

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

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

Okay So you will know the whole key word and it will be at the beginning , right ?

Keyword is stored in an variable and whem i m mentioning it with * i m getting error

yess

@Doonline ,

What is the Error that you have received?

Could you provide us a Screenshot ?

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

formate is not same it can be “_” or “-” but the keyword is same and unique

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

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