Question
· Oct 14, 2020

ObjectScript - Appending to a file from another

–  I want to “copy” a file to another, appending if the file already exists. The below code works, except it overwrites the new file, rather than append.  The documentation says CopyFile will append

 

//Both files exists, so we can combine them
                               
                 set tsc=##class(%File).CopyFile("D:\Files\Out\Vigilanz\AD_Lawson10132020.txt","D:\Files\Out\Vigilanz\employee10132020.txt"
                                if $$$ISOK(tsc)
                                {
                                !, "SQL file copied"
                                }
                                else 
                                {
                                Quit $$$ERROR($$$GeneralError,"SQL file not copied: "_SQLFileDate_" to "_EmployeeFile)
                                }

 

Thanks, Nora

Discussion (2)1
Log in or sign up to continue

Hi Nora! Long time laugh

Something like this should do the trick, assuming the file to be appended to is named "spoo.txt" and the file from which you're appending is named "fleem.txt":

    Set tOut = ##class(%File).%New()
    Set tIn = ##class(%File).%New()
    Set tSC = 1
    Set tOut.Name = "spoo.txt"
    Set tIn.Name = "fleem.txt"
    Set sc = tOut.Open("WA")
    Set:$$$ISERR(sc) tSC=$$$ADDSC(tSC,sc)
    Set sc = tIn.Open()
    Set:$$$ISERR(sc) tSC=$$$ADDSC(tSC,sc)
    Quit:$$$ISERR(tSC) tSC
    While 'tIn.AtEnd
    {
        Set sc=tOut.Write(tIn.Read())
        Return:$$$ISERR(sc) sc
    }
    Do tIn.Close()
    Do tOut.Close()
    Return $$$OK