How to search file name in folder…here file name is “TA charges dated 2021-09-08 2135449484” where date is in the format of YYYY-MM-dd and then invoice number…but here to search file name I have only information of year and month which vary every month I.e 2021-09…so how I can search file name upto this value only i.e TA charges dated 2021-09 and then I will get full file name?
Hi @rutuja.y, how many files you may have with file name starts with “TA charges dated 2021-09” ?
Only one…every month only one file is saved into that folder…other files are like…2021-08 according to the month…yyyy-MM
Use Directory.GetFiles("< Path >") → Returns you the list of all files.
Loop through the list and with the If condition use Contains method to find the file name.
Else you can get the list of the latest file by:
Directory.GetFiles(our_FolderPath,”*”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1) )
arrFilePaths | DataType: String( ) - a String Array
new DirectoryInfo("YourPath").GetFiles("TA charges*.*").Where(Function (x) x.Name.StartsWith("TA charges dated " & strFilter)).Select(Function (x) x.FullName).toArray
with a variable strFilter having the a value to the searched month
strFilter = “2021-08”
or dynamic to this month
strFilter = now.toString(“yyyy-MM”)
we can check the result with arrFilePaths .Count and when it is 1 we can get the file name with arrFilePaths(0)
How to apply loop for that?and if condition
Here you go:
Or simply you can follow @ppr’s suggestion where no need of using Foreach and If activities.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.