在VBS中的某些行的末尾添加某些文本

时间:2017-08-23 06:50:43

标签: vbscript data-manipulation find-replace

我有一个.txt文件,我希望在VBS中编辑。数据如下:

Time, Column 1, Column 2
23/08/2017 8:30:00 AM, Data, Data
23/08/2017 8:35:00 AM, Data, Data
23/08/2017 8:40:00 AM, Data, Data
23/08/2017 8:45:00 AM, Data, Data

我想要的是另一个专栏'在第一行的末尾添加了名为 batch ,然后是第一个时间值(23/08/2017 8:30:00 AM)来组成此列的数据,以便最终结果如下所示:

Time, Column 1, Column 2, Batch
23/08/2017 8:30:00 AM, Data, Data, 23/08/2017 8:30:00 AM
23/08/2017 8:35:00 AM, Data, Data, 23/08/2017 8:30:00 AM
23/08/2017 8:40:00 AM, Data, Data, 23/08/2017 8:30:00 AM
23/08/2017 8:45:00 AM, Data, Data, 23/08/2017 8:30:00 AM

注意每列之间存在逗号分隔符。

2 个答案:

答案 0 :(得分:0)

您可以这样做:

  • 逐行开始从文件中读取数据,并将其与要添加到每行末尾的数据一起存储在临时变量中。
  • 对于第一行,要追加的数据是“,批处理”,对于其余行,要追加的数据是第二行的“时间值”。
  • 以写入模式打开文件并写入存储在该临时变量中的数据

代码:

strPath = "C:\Users\gr.singh\Desktop\Desktop\Gurman\2017\as.txt"    'Replace this path with your file path
Set fso = CreateObject("scripting.filesystemobject")
Set rfile = fso.OpenTextFile(strPath,1)                'File opened in Read-only mode
While Not rfile.AtEndOfStream
    temp=rfile.ReadLine()
    If rfile.Line=2 Then                               'The first line has been read by using the readline method due to which rfile.line gets set to 2. Hence, I have used 2 here for the 1st line. Similarly, I have used 3 fro the 2nd line in the ElseIf Condition
        dataToAppend = "Batch"
    ElseIf rfile.Line=3 Then
        dataToAppend = Split(temp,",")(0)
    End If
    fulldata = fulldata & temp&", "&dataToAppend&"||"
Wend
rfile.Close
fulldata = Left(fulldata,Len(fulldata)-2)
Set wfile = fso.OpenTextFile(strPath,2)                'File opened in write mode
tempArr = Split(fulldata,"||")
For i=0 To UBound(tempArr)
    wfile.WriteLine tempArr(i)
Next
wfile.Close
Set fso= Nothing

输出: enter image description here

答案 1 :(得分:0)

如果您的文件相当小,您可以将其作为一个整体阅读并按如下方式处理:

filename = "C:\path\to\your.txt"

Set fso = CreateObject("Scripting.FileSystemObject")

txt  = fso.OpenTextFile(filename).ReadAll
data = Split(txt, vbNewLine)

If UBound(data) >= 0 Then data(0) = data(0) & ",batch"
If UBound(data) >= 1 Then
  batchval = Left(data(1), InStr(data(1), ",")-1)
  data(1) = data(1) & "," & batchval
End If
For i = 2 To UBound(data)
  data(i) = data(i) & "," & batchval
Next

fso.OpenTextFile(filename, 2).Write Join(data, vbNewLine)

对于大文件,不建议采用这种方法,因为它可能导致内存耗尽,导致计算机停止运行。如果您的文件很大,您最好逐行处理文件,将输出写入临时文件,并在完成后替换原始文件。

filename    = "C:\path\to\your.txt"
tmpfilename = filename & ".tmp"

Set fso = CreateObject("Scripting.FileSystemObject")

inFile  = fso.OpenTextFile(filename)
outFile = fso.OpenTextFile(tmpfilename, 2, True)

If Not inFile.AtEndOfStream Then outFile.WriteLine inFile.ReadLine & ",batch"
If Not inFile.AtEndOfStream Then
  line = inFile.ReadLine
  batchval = Left(line, InStr(line, ",")-1)
  outFile.WriteLine line & "," & batchval
End If
Do Until inFile.AtEndOfStream
  outFile.WriteLine inFile.ReadLine & "," & batchval
Loop

inFile.Close
outFile.Close

fso.DeleteFile filename, True
fso.MoveFile tmpfilename, filename