这里是新手。我在VB中有以下代码用于读取csv文件。文件名包括当前日期,因此每天都会更改。如何使用正则表达式表示文件名,以便读取任何csv文件?感谢
Dim objReader = New IO.StreamReader("\FolderAFolderBSEBCTS20220831.csv")
我试过了-
objReader = New IO.StreamReader("\FolderAFolderB^SEBCTSwwwwwwww.csv$")
'objReader = New IO.StreamReader("\FolderAFolderB^wwwwwwwwwwwwww.csv$")
但我得到了一个错误-
错误:找不到路径"\FolderA\FolderB^SEBCTS\w\w\w \w \w\w.csv$"的一部分。
为什么需要regex,请使用Date.TryParseExact
和Path.GetFileNameWithoutExtension
:
Dim file = "\FolderAFolderBSEBCTS20220831.csv"
Dim fileName = Path.GetFileNameWithoutExtension(file)
Dim format = "yyyyMMdd"
Dim possibleDate = If(fileName.Length >= format.Length, fileName.Substring(fileName.Length - format.Length), Nothing)
Dim parsedDate As Date
Dim isValidFile = Date.TryParseExact(possibleDate, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, parsedDate)
如果你甚至不想确保文件名的最后几位是有效的日期,但今天也是:
isValidFile = isValidFile AndAlso parsedDate.Date = Date.Today