我有一个看起来像这样的文件:
\\srv10219\e\Archive\AllocationListFile\20140805_152774 \\srv10219\e\Archive\AllocationListFile\20140925_132714 \\srv10219\e\Archive\AllocationListFile\20140925_133144 \\srv10219\e\Archive\AllocationListFile\20140925_133601 \\srv10219\e\Archive\AllocationListFile\20141104_145839 \\srv10219\e\Archive\AllocationListFile\20151023_104951 \\srv10219\e\Archive\AllocationListFile\20151027_072235 \\srv10219\e\Archive\Article\20140825_121209 \\srv10219\e\Archive\Article\20140825_121759 \\srv10219\e\Archive\Article\20140825_123400 \\srv10219\e\Archive\Article\20140827_065126 \\srv10219\e\Archive\Article\20141006_094447 \\srv10219\e\Archive\Article\20141006_110546 \\srv10219\e\Archive\Article\20141008_105947 \\srv10219\e\Archive\Article\20150917_093710 \\srv10219\e\Archive\Article\20151005_190254 \\srv10219\e\Archive\Article\20151205_190255 \\srv10219\e\Archive\Article\20161005_190253
我只需要读取文件中字符串末尾的日期文件夹值大于或等于(date1 = 20140925)或小于等于(date2 = 20151005)的那些行。所以我的输出应该是这样的:
\\srv10219\e\Archive\AllocationListFile\20140925_132714 \\srv10219\e\Archive\AllocationListFile\20140925_133144 \\srv10219\e\Archive\AllocationListFile\20140925_133601 \\srv10219\e\Archive\AllocationListFile\20141104_145839 \\srv10219\e\Archive\Article\20141006_094447 \\srv10219\e\Archive\Article\20141006_110546 \\srv10219\e\Archive\Article\20141008_105947 \\srv10219\e\Archive\Article\20150917_093710 \\srv10219\e\Archive\Article\20151005_190254
我知道这可以通过InStr
函数实现,但不能将它们组合在一起。我尝试了以下代码:
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If (InStr(line, date1) > 0 Or InStr(line, date2) > 0 ) Then writeOutput = True
If writeOutput Then outFile.WriteLine line
If InStr(line, date2) > 0 Then Exit Do
Loop
这给我输出
\\srv10219\e\Archive\AllocationListFile\20140925_132714 \\srv10219\e\Archive\AllocationListFile\20140925_133144 \\srv10219\e\Archive\AllocationListFile\20140925_133601 \\srv10219\e\Archive\AllocationListFile\20141104_145839 \\srv10219\e\Archive\AllocationListFile\20151023_104951 \\srv10219\e\Archive\AllocationListFile\20151027_072235 \\srv10219\e\Archive\Article\20140825_121209 \\srv10219\e\Archive\Article\20140825_121759 \\srv10219\e\Archive\Article\20140825_123400 \\srv10219\e\Archive\Article\20140827_065126 \\srv10219\e\Archive\Article\20141006_094447 \\srv10219\e\Archive\Article\20141006_110546 \\srv10219\e\Archive\Article\20141008_105947 \\srv10219\e\Archive\Article\20150917_093710 \\srv10219\e\Archive\Article\20151005_190254
下面的行是不需要的行,你可以看到它从头到尾采用日期范围,而我希望它采取文件夹方式。
\\srv10219\e\Archive\AllocationListFile\20151023_104951 \\srv10219\e\Archive\AllocationListFile\20151027_072235 \\srv10219\e\Archive\Article\20140825_121209 \\srv10219\e\Archive\Article\20140825_121759 \\srv10219\e\Archive\Article\20140825_123400 \\srv10219\e\Archive\Article\20140827_065126
答案 0 :(得分:1)
InStr
用于完全匹配。如果要匹配一个范围,则需要从字符串中提取值并在进行比较之前将其转换为整数,例如使用正则表达式:
date1 = 20140925
date2 = 20151005
Set re = New RegExp
re.Pattern = "\\(\d{8})_\d{6}$"
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
For Each m In re.Execute(line)
d = CLng(m.Submatches(0))
Next
If d >= date1 And d <= date2 Then outFile.WriteLine line
Loop