Text 使用vbscript修改文本文件

Text 使用vbscript修改文本文件,text,vbscript,line,Text,Vbscript,Line,每天我们都会得到一个平面文本文件。有时,文件中的某些行需要在处理之前删除。这些行可以出现在不同的位置,但始终以6999或7999字符开头。我们希望运行一个脚本来删除这些特定的行。然而,这远远超出了我的理解,任何一个以6999开头的行,在它前面都会有一个以5442开头的行,这也需要删除,但前提是它出现在6999行之前 我们是一家Windows商店,将在Windows中作为简单批处理文件的一部分运行此脚本。我们不使用Unix或Linux,也不希望使用它 文件扩展名反映了日期。今天的文件是file.1

每天我们都会得到一个平面文本文件。有时,文件中的某些行需要在处理之前删除。这些行可以出现在不同的位置,但始终以6999或7999字符开头。我们希望运行一个脚本来删除这些特定的行。然而,这远远超出了我的理解,任何一个以6999开头的行,在它前面都会有一个以5442开头的行,这也需要删除,但前提是它出现在6999行之前

我们是一家Windows商店,将在Windows中作为简单批处理文件的一部分运行此脚本。我们不使用Unix或Linux,也不希望使用它

文件扩展名反映了日期。今天的文件是file.100621,明天的文件是file.100622。我在这方面遇到了问题,因为vbscript似乎不喜欢这个文件*

以下是文本文件的示例:

4006006602    03334060000100580                                                 
40060066039    0334070000100580                                                 
700600000011571006210060001255863                                               
544264287250111000025000000000040008000801                                      
6999001000000000000000000000000000000000000000000000000000                      
6999001000000000000000000000000000000000000000000000000000                      
6999001000000000000000000000000000000000000000000000000000                      
799900000011571006210030000000000                                               
8007000000115710062102530054008920  
我们想删除这个文件中的5行(5442行、3行6999行和7999行)

这是我在这个网站上找到的脚本的一个示例,经过修改并取得了一些成功,但不知道如何删除行(只知道如何替换行中的数据)。我意识到这要么需要重大修改,要么需要完全抛弃,但我发布这篇文章是为了提供一个我认为我们正在寻找的想法。我将其与cscript.exe放在一个目录中,并从一个简单的批处理文件调用它:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\temp\file.100621"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine,"6999")> 0 Then
        strLine = Replace(strLine,"6999","delete line")
    End If 
    WScript.Echo strLine
Loop
这让我想到:

40060066039    0334070000100580                                                 
700600000011571006210060001255863                                               
544264287250111000025000000000040008000801                                      
delete line001000000000000000000000000000000000000000000000000000                      
delete line001000000000000000000000000000000000000000000000000000                      
delete line001000000000000000000000000000000000000000000000000000                      
799900000011571006210030000000000                                               
8007000000115710062102530054008920  
接近!只需要删除行,而不是写“删除行”。 根据我所知,以下是我的具体需求:

  • 获取脚本以处理目录中的任何文件(一次只能处理1个文件,但扩展名每天都在更改)
  • 让脚本删除以5442开头的任何行,该行紧靠以6999开头的行之前
  • 让脚本完全删除以6999和7999开头的行
  • 我认为这会起作用(但我不太擅长VBS,所以没有承诺):


    注意,我想你是在检查线是否包含在任何地方,但是你说规则是从数字开始的,这就是为什么我做了<代码> 1 >代码>而不是<代码> 0 <代码> .< /p> < p>我做了一些修改,试图消除空白行,我还添加了一个函数来循环输出文件并删除任何空行。希望这个能起作用

    Select Case Wscript.Arguments.Count
        case 1:
            strInput = GetFile(WScript.Arguments(0))
            RemoveUnwantedLines strInput, strInput
            RemoveBlankLines strInput
        case 2:
            strInput = GetFile(WScript.Arguments(0))
            strOutput = Wscript.Arguments(1)
            RemoveUnwantedLines strInput, strOutput
            RemoveBlankLines strOutput
    End Select
    
    Function GetFile(strDirectory)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(strDirectory)
        dateLastModified = Null
        strFile = ""
        For Each objFile in objFolder.Files
            If IsNull(dateLastModified) Then
                dateLastModified = objFile.DateLastModified
                strFile = objFile.Path
            ElseIf dateLastModified < objFile.DateLastModified Then
                dateLastModified = objFile.DateLastModified
                strFile = objFile.Path
            End If
        Next
        GetFile = strFile
    End Function
    
    Sub RemoveUnwantedLines(strInputFile, strOutputFile)
            'Open the file for reading.
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,1)
            'Read the entire file into memory.
        strFileText = objFile.ReadAll
            'Close the file.
        objFile.Close
            'Split the file at the new line character. *Use the Line Feed character (Char(10))
        arrFileText = Split(strFileText,Chr(10))
            'Open the file for writing.
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strOutputFile,2,true)
            'Loop through the array of lines looking for lines to keep.
        For i = LBound(arrFileText) to UBound(arrFileText)
                'If the line is not blank process it.
            If arrFileText(i) <> "" Then
                    'If the line starts "5442", see if the next line is "6999".
                If Left(arrFileText(i),4) = "5442" Then
                        'Make sure the next line exists (Don't want an out of bounds exception).
                    If i + 1 <= UBound(arrFileText)Then
                            'If the next line is not "6999" 
                        If Left(arrFileText(i + 1), 4) <> "6999" Then
                                'Write the "5442" line to the file.
                            objFile.WriteLine(arrFileText(i))
                        End If
                    Else
                            'If the next line does not exist, write the "5442" line to the file (without a new line).
                        objFile.WriteLine(arrFileText(i))
                    End If              
                    'If the line does not start with "6999" and the line does not start with "7999".
                Elseif Left(arrFileText(i),4) <> "6999"  AND Left(arrFileText(i),4) <> "7999" Then
                        'Write the line to the file.
                    objFile.WriteLine(arrFileText(i))
                End If
            End If
        Next
            'Close the file.
        objFile.Close
        Set objFile = Nothing
    End Sub
    
    Sub RemoveBlankLines(strInputFile)
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,1)
            'Read the entire file into memory.
        strFileText = objFile.ReadAll
            'Close the file.
        objFile.Close
            'Split the file at the new line character.
        arrFileText = Split(strFileText,VbNewLine)
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,2,true)
            'Loop through the array of lines looking for lines to keep.
        For i = LBound(arrFileText) to UBound(arrFileText)
                'If the line is not blank.
            if arrFileText(i) <> "" Then
                    'If there is another element.
                if i + 1 <= UBound(arrFileText) Then    
                        'If the next element is not blank.
                    if arrFileText(i + 1) <> "" Then
                            'Write the line to the file.
                        objFile.WriteLine(arrFileText(i))
                    Else
                            'Write the line to the file (Without a blank line).
                        objFile.Write(arrFileText(i))
                    End If
                Else
                        'Write the line to the file (Without a blank line).
                    objFile.Write(arrFileText(i))
                End If
            End If
        Next
        'Close the file.
        objFile.Close
        Set objFile = Nothing
    End Sub 
    


    这将是我解决此问题的伪算法:

    (我宁愿教你我如何解决它的想法,也不愿提供代码本身)

  • 将该文件用作参数(以便灵活使用)或创建一个“后台处理程序”文件夹,该程序在运行时检查新内容,如邮件的“收件箱”。然后你还需要一个“发件箱”。这样,您就可以在文件出现时处理它们,而不知道它们的名称,并在处理时将它们移动到“发件箱”中

  • 为这个程序也制作一个简单的“配置”文件。每一行都可以表示“过滤器”,如果需要的话,稍后您还可以向这些行添加操作

    7999删除

    6999删除

    5442删除

    如[模式][行动]

  • 现在,在将配置读入一组“键”之后,检查“收件箱”中的文件。 对于每个文件,使用密钥集处理它

  • 处理文件“xxxxxxxx.log”(或任何名称) 加载所有行,如果没有太多的或readline来获取单个行(取决于性能和内存使用)

  • 对于每行,从字符串中取出前4个字母

  • 现在我们需要一行代码来解析:

    sLine = left(readline(input filestream), 4) 
    
    因为我们只需要前4个字符来决定是否需要保留它

    如果这个“sLine”(字符串)在我们的过滤器/模式数组中,那么我们有一个匹配项。。。执行我们配置的操作(在当前设置中-delete=ignore行)

    6a。如果忽略,则转到文本文件中的下一行,转到第7行

    6b。如果阵列中没有匹配项,那么我们需要保留一条线。将其写入输出流

  • 如果有更多行,下一步(转到第5行)

  • 关闭输入和输出文件

  • 从收件箱中删除/移动输入文件(可能要备份?)

  • 如果目录[inbox]中有更多文件,请解析下一步。。。转到第4页

  • 这不仅仅是纯VBSCRIPT,而是任何语言的ann算法思想


    我希望你能从中看到我的想法,否则你只需发表评论,我将尝试详细阐述。希望我给了你一个很好的答案。

    好的,这是由Tester101精心编写的最终脚本。此脚本将删除不需要的行,如上所述。它还处理每行末尾的换行符(我不知道)

    选择Case Wscript.Arguments.Count
    案例1:
    strInput=GetFile(WScript.Arguments(0))
    移除未插入的线,竖直,竖直
    删除空白线划线
    案例2:
    strInput=GetFile(WScript.Arguments(0))
    strOutput=Wscript.Arguments(1)
    拆下未连接的线路,包括输入线、输出线
    删除空白行输出
    结束选择
    函数GetFile(strDirectory)
    设置objFSO=CreateObject(“Scripting.FileSystemObject”)
    设置objFolder=objFSO.GetFolder(strDirectory)
    dateLastModified=Null
    strFile=“”
    对于objFolder.Files中的每个objFile
    如果为空(dateLastModified),则
    dateLastModified=objFile.dateLastModified
    strFile=objFile.Path
    ElseIf dateLastModifiedRemoveUnwantedLines "C:\TestDirectory\" "C:\Output.txt"
    
    RemoveUnwantedLines "C:\TestDirectory\"
    
    sLine = left(readline(input filestream), 4) 
    
    Select Case Wscript.Arguments.Count
    case 1:
        strInput = GetFile(WScript.Arguments(0))
        RemoveUnwantedLines strInput, strInput
        RemoveBlankLines strInput
    case 2:
        strInput = GetFile(WScript.Arguments(0))
        strOutput = Wscript.Arguments(1)
        RemoveUnwantedLines strInput, strOutput
        RemoveBlankLines strOutput
    End Select
    
    Function GetFile(strDirectory)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strDirectory)
    dateLastModified = Null
    strFile = ""
    For Each objFile in objFolder.Files
        If IsNull(dateLastModified) Then
            dateLastModified = objFile.DateLastModified
            strFile = objFile.Path
        ElseIf dateLastModified < objFile.DateLastModified Then
            dateLastModified = objFile.DateLastModified
            strFile = objFile.Path
        End If
    Next
    GetFile = strFile
    End Function
    
    Sub RemoveUnwantedLines(strInputFile, strOutputFile)
        'Open the file for reading.
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,1)
        'Read the entire file into memory.
        strFileText = objFile.ReadAll
        'Close the file.
         objFile.Close
        'Split the file at the new line character. *Use the Line Feed character (Char(10))
        arrFileText = Split(strFileText,Chr(10))
        'Open the file for writing.
        Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strOutputFile,2,true)
        'Loop through the array of lines looking for lines to keep.
        For i = LBound(arrFileText) to UBound(arrFileText)
            'If the line is not blank process it.
            If arrFileText(i) <> "" Then
                'If the line starts "5442", see if the next line is "6999".
                If Left(arrFileText(i),4) = "5442" Then
                    'Make sure the next line exists (Don't want an out of bounds exception).
                    If i + 1 <= UBound(arrFileText)Then
                        'If the next line is not "6999" 
                        If Left(arrFileText(i + 1), 4) <> "6999" Then
                            'Write the "5442" line to the file.
                            objFile.WriteLine(arrFileText(i))
                        End If
                    Else
                        'If the next line does not exist, write the "5442" line to the file (without a new line).
                        objFile.WriteLine(arrFileText(i))
                    End If              
                'If the line does not start with "6999" and the line does not start with "7999".
                Elseif Left(arrFileText(i),4) <> "6999"  AND Left(arrFileText(i),4) <> "7999" Then
                    'Write the line to the file.
                    objFile.WriteLine(arrFileText(i))
                End If
            End If
    Next
        'Close the file.
    objFile.Close
    Set objFile = Nothing
    End Sub
    
    Sub RemoveBlankLines(strInputFile)
    Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,1)
        'Read the entire file into memory.
    strFileText = objFile.ReadAll
        'Close the file.
    objFile.Close
        'Split the file at the new line character.
    arrFileText = Split(strFileText,VbNewLine)
    Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strInputFile,2,true)
        'Loop through the array of lines looking for lines to keep.
    For i = LBound(arrFileText) to UBound(arrFileText)
            'If the line is not blank.
        if arrFileText(i) <> "" Then
                'If there is another element.
            if i + 1 <= UBound(arrFileText) Then    
                    'If the next element is not blank.
                if arrFileText(i + 1) <> "" Then
                        'Write the line to the file.
                    objFile.WriteLine(arrFileText(i))
                Else
                        'Write the line to the file (Without a blank line).
                    objFile.Write(arrFileText(i))
                End If
            Else
                    'Write the line to the file (Without a blank line).
                objFile.Write(arrFileText(i))
            End If
        End If
    Next
    'Close the file.
    objFile.Close
    Set objFile = Nothing
    End Sub