Text 使用vbscript修改文本文件
每天我们都会得到一个平面文本文件。有时,文件中的某些行需要在处理之前删除。这些行可以出现在不同的位置,但始终以6999或7999字符开头。我们希望运行一个脚本来删除这些特定的行。然而,这远远超出了我的理解,任何一个以6999开头的行,在它前面都会有一个以5442开头的行,这也需要删除,但前提是它出现在6999行之前 我们是一家Windows商店,将在Windows中作为简单批处理文件的一部分运行此脚本。我们不使用Unix或Linux,也不希望使用它 文件扩展名反映了日期。今天的文件是file.100621,明天的文件是file.100622。我在这方面遇到了问题,因为vbscript似乎不喜欢这个文件* 以下是文本文件的示例:Text 使用vbscript修改文本文件,text,vbscript,line,Text,Vbscript,Line,每天我们都会得到一个平面文本文件。有时,文件中的某些行需要在处理之前删除。这些行可以出现在不同的位置,但始终以6999或7999字符开头。我们希望运行一个脚本来删除这些特定的行。然而,这远远超出了我的理解,任何一个以6999开头的行,在它前面都会有一个以5442开头的行,这也需要删除,但前提是它出现在6999行之前 我们是一家Windows商店,将在Windows中作为简单批处理文件的一部分运行此脚本。我们不使用Unix或Linux,也不希望使用它 文件扩展名反映了日期。今天的文件是file.1
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 >代码>而不是<代码> 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
或
这将是我解决此问题的伪算法: (我宁愿教你我如何解决它的想法,也不愿提供代码本身)
sLine = left(readline(input filestream), 4)
因为我们只需要前4个字符来决定是否需要保留它
如果这个“sLine”(字符串)在我们的过滤器/模式数组中,那么我们有一个匹配项。。。执行我们配置的操作(在当前设置中-delete=ignore行)
6a。如果忽略,则转到文本文件中的下一行,转到第7行
6b。如果阵列中没有匹配项,那么我们需要保留一条线。将其写入输出流
我希望你能从中看到我的想法,否则你只需发表评论,我将尝试详细阐述。希望我给了你一个很好的答案。好的,这是由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