读取文本并匹配文本文件中的所有日期,否则将值写入 error.txt 文件 [英] Read text and match all dates in text file otherwise write value to error.txt file

查看:17
本文介绍了读取文本并匹配文本文件中的所有日期,否则将值写入 error.txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 .TXT 文件被读入 VBS FileSystemObject.我正在尝试搜索所有匹配的日期,否则我需要将它们放在error.txt"文件中.但是,当我在下面运行我的代码时,它总是将匹配项放在 error.txt 文件中,而不是跳过匹配的日期.

The below .TXT file is read into a VBS FileSystemObject. I am trying to search for all dates that match otherwise I need to put them in a "error.txt" file. However, when I run my code below it is always placing the matches in the error.txt file and not skipping the matching dates.

为什么日期不匹配?

输入:

"LIRRR 1M",.412900,02/08/2016
"LIRRR 3M",.222700,02/08/2016
"LIRRR 6M",.333200,02/08/2016
"LIRRR12M",1.1333300,02/08/2016
"FEDFRRRR",.333000,02/08/2016
"CCC 1YR",.550330,02/08/2016
"5YRCMT",1.2503300,02/08/2016
"10YRCMT",1.860000,02/08/2016 

这是我写的代码:

On error resume next
Const ForReading = 1
Dim strSearchFor
Dim MyDate, MyWeekDay
MyDate = Date ' Assign a date.
MyWeekDay = Weekday(MyDate) 
If MyWeekDay = 2 then 
  strSearchFor =Right("0" & DatePart("m",Date), 2)&"/"&Right("0" & DatePart("d",Date-3), 2)&"/"&DatePart("yyyy",Date)
 Else 
  strSearchFor =Right("0" & DatePart("m",Date), 2)&"/"&Right("0" & DatePart("d",Date-1), 2)&"/"&DatePart("yyyy",Date)
 End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Users\Desktop\index.txt", ForReading)
do until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine()

    If InStr(strLine, strSearchFor) <> 0 then

    Set objFile = objFSO.CreateTextFile("C:\Users\Desktop\pass.txt")
      objFile.Write "date is  match"& vbCrLf

    Else
        Set objFile = objFSO.CreateTextFile("C:\Users\Desktop\error.txt")
      objFile.Write "date is not match"& vbCrLf
    End If
loop
objTextFile.Close

推荐答案

为什么不使用 RegEx 来获取显示为日期的字符串部分并使用 IsDate 验证它的函数?

Why not use RegEx to get the portion of the string that appears to be the date and use the IsDate Function to validate it?

Option Explicit
Dim arrLines,i
arrLines = ReadFile("./input.txt","byline")
For i=LBound(arrLines) to UBound(arrLines)
    wscript.echo FormatOutput(arrLines(i))
Next
'*********************************************
Function FormatOutput(s)
    Dim re, match
    Set re = New RegExp
    re.Pattern = "[\d]+[\/-][\d]+[\/-][\d]+"
    re.Global = True
    For Each match In re.Execute(s)
        if IsDate(match.value) then
            FormatOutput = CDate(match.value)
            Exit For
        end if
    Next
    Set re = Nothing
End Function
'*********************************************
Function ReadFile(path,mode)
    Const ForReading = 1
    Dim objFSO,objFile,i,strLine
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(path,ForReading)
    If mode = "byline" then
        Dim arrFileLines()
        i = 0
        Do Until objFile.AtEndOfStream
            Redim Preserve arrFileLines(i)
            strLine = objFile.ReadLine
            strLine = Trim(strLine)
            If Len(strLine) > 0 Then
                arrFileLines(i) = strLine
                i = i + 1
                ReadFile = arrFileLines
            End If  
        Loop
        objFile.Close
    End If
    If mode = "all" then
        ReadFile = objFile.ReadAll
        objFile.Close
    End If
End Function
'*****************************************************************

这篇关于读取文本并匹配文本文件中的所有日期,否则将值写入 error.txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆