每天我们都会得到一个纯文本文件。有时文件中的某些行需要在处理之前删除。这些行可以出现在不同的地方,但总是以字符 6999 或 7999 开头。我们想运行一个脚本来删除这些特定的行。然而,这超出了我的范围,任何有一行以 6999 开头的地方都会有一行以 5442 开头,也需要删除,但前提是它出现在 6999 行之前。
我们是一家 Windows 商店,并将将此脚本作为 Windows 中简单批处理文件的一部分运行。我们不使用 Unix 或 Linux,也不想使用。
文件扩展名反射(reflect)了日期。今天的文件是file.100621,明天的文件是file.100622。我在这方面遇到了麻烦,因为 vbscript 似乎不喜欢 file.*
这是文本文件的示例:
4006006602 03334060000100580
40060066039 0334070000100580
700600000011571006210060001255863
544264287250111000025000000000040008000801
6999001000000000000000000000000000000000000000000000000000
6999001000000000000000000000000000000000000000000000000000
6999001000000000000000000000000000000000000000000000000000
799900000011571006210030000000000
8007000000115710062102530054008920
我们想删除此文件中的 5 行(5442 行、三个 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
关闭!只需要删除行而不是写“删除行”。
因此,根据我的了解,以下是我的具体需求:
最佳答案
我做了一些更改以尝试消除空行,我还添加了一个函数来循环输出文件并删除任何空行。希望这个有效。
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
要使用它,可以通过以下两种方式之一从命令行调用它。
RemoveUnwantedLines "C:\TestDirectory\" "C:\Output.txt"
或者
RemoveUnwantedLines "C:\TestDirectory\"
关于text - 使用 vbscript 修改文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3097205/