Split filename inside For each file in a folder

Hi everyone,

I have a folder containing bunch of files with naming (Report_BuName.xlsx). I am looping through each file in a folder to preform some actions where I require the BuName of each file.

Thanks in advance!

Hi @Rupesh_Parle

Try this

System.Text.RegularExpressions.Regex.Match(Input.ToString,"(?<=Report_)(\w+)").Value

Cheers!!

@Rupesh_Parle

You’re using for each file in folder so take input as Currentfile.Name

Or

Directly you can give this
Currentfile.Name.Split(“_”)(1).Split(“.”)(0)

Input="Report_BuName.xlsx"
Input.Split("_")(1).Split(".")(0)

Hope this helps!!

1 Like

Hi,

FYI, another approach:

 System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(CurrentFile.Name),"[^_]+$").Value

Regards,

1 Like

@Rupesh_Parle

Inside the for each file in folder use the following

CurrentFile.Name.Split({"_","."},3,StringSplitOptions.None)(1)

Hope this helps

Cheers

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