Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to text file if it already exists

Tags:

vbscript

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

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
like image 617
Dimitar Dimitrov Avatar asked May 27 '15 09:05

Dimitar Dimitrov


People also ask

How do I append to an existing file?

Example 2: Append text to an existing file using FileWriter When creating a FileWriter object, we pass the path of the file and true as the second parameter. true means we allow the file to be appended. Then, we use write() method to append the given text and close the filewriter.

How do you append text into an existing file in Java?

You can append text into an existing file in Java by opening a file using FileWriter class in append mode. You can do this by using a special constructor provided by FileWriter class, which accepts a file and a boolean, which if passed as true then open the file in append mode.


1 Answers

Open the file for appending

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 
like image 195
MC ND Avatar answered Nov 12 '22 14:11

MC ND