如果文本文件已经存在,则将文本附加到文本文件 [英] Append text to text file if it already exists

查看:36
本文介绍了如果文本文件已经存在,则将文本附加到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的脚本,用于替换固定宽度文件中的某些字符(从第 2 行开始).

I have a script which is working, to replace some characters in a fixed width file (starting from row 2 onward).

如果目标文件已经存在,希望避免覆盖它.相反,如果它存在,则将行(从源文件的第 2 行开始)附加到目标文件的末尾.我正在努力寻找一个带有适当建议的线程.这是当前代码:

What want to avoid overwriting the target file if it already exists. Instead, if it exists, to append the rows (from row 2 onwards of the source file) to the end of the target file. I am struggling to find a thread with a proper suggestion. This is the current code:

Dim objFSO
dim objFile
dim thisLine
Set objFSO = CreateObject("Scripting.FileSystemObject")


If (objFSO.FileExists("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")) Then
  Set objFile = objFSO.GetFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")
Else
  WScript.Quit()
End If

If objFile.Size > 0 Then 'make sure the input file is not empty
    Set inputFile = objFSO.OpenTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat", 1)  'Replace the filename here
    set outputFile = objFSO.CreateTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYIMP.dat", TRUE) 'replace it with output filename

    ' first line - leave it as it is
    thisLine = inputFile.ReadLine  
    newLine = thisLine
    outputFile.WriteLine newLine

    'all remaining lines - read them and replace the middle part with 18 zeroes 
    do while not inputFile.AtEndOfStream  
            thisLine = inputFile.ReadLine  ' Read an entire line into a string.
            'the zeroes are to fix issue N1 (payment in other amt)
            'the CDF are to fix issue N2 (payment in local amt)
            newLine = mid(thisLine,1,47) & "000000000000000000" & mid(thisLine,66,121) & "CDF" & mid(thisLine,190)
            outputFile.WriteLine newLine
    loop
    inputFile.Close
    outputFile.Close
    objFSO.DeleteFile "C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat"
end if

推荐答案

打开文件进行追加

Option Explicit

Const ForReading = 1, ForAppending = 8

Dim inputFileName, outputFileName
    inputFileName  = "C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat"
    outputFileName = "C:\Users\Dimitar\Desktop\BPSDRC\PAYIMP.dat"

Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FileExists( inputFileName ) Then
        WScript.Quit
    End If

    If fso.GetFile( inputFileName ).Size < 1 Then 
        WScript.Quit
    End If 

Dim newFile, inputFile, outputFile 
    newFile = Not fso.FileExists( outputFileName )

    Set inputFile = fso.OpenTextFile( inputFileName, ForReading )
    Set outputFile = fso.OpenTextFile( outputFileName, ForAppending, True )

Dim lineBuffer

    lineBuffer = inputFile.ReadLine()
    If newFile Then 
        outputFile.WriteLine lineBuffer
    End If

    Do While Not inputFile.AtEndOfStream  
        lineBuffer = inputFile.ReadLine
        lineBuffer = mid(lineBuffer,1,47) & "000000000000000000" & mid(lineBuffer,66,121) & "CDF" & mid(lineBuffer,190)
        outputFile.WriteLine lineBuffer
    Loop

    inputFile.Close
    outputFile.Close

    fso.DeleteFile inputFileName 

这篇关于如果文本文件已经存在,则将文本附加到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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