Showing posts with label append text. Show all posts
Showing posts with label append text. Show all posts

Tuesday, February 7, 2017

VBScript Append text to text file if it already exists

Option Explicit

Const ForReading = 1, ForAppending = 8

Dim InputFileName, OutputFileName, FileSystemObject, InputFile, OutputFile

Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
InputFileName  = "C:\tmp\input.txt"
OutputFileName = "C:\tmp\output.txt"

PreCheck

Set InputFile = FileSystemObject.OpenTextFile(InputFileName, ForReading)
Set OutputFile = FileSystemObject.OpenTextFile(OutputFileName, ForAppending, True)

OutputFile.WriteLine ""
Do While Not InputFile.AtEndOfStream 
    OutputFile.WriteLine InputFile.ReadLine
Loop

InputFile.Close
OutputFile.Close

Sub Echo(Str)
    Wscript.Echo Str
End Sub

Sub PreCheck()
    If Not FileSystemObject.FileExists(OutputFileName) Then
        Echo "Output File Not Found"
        WScript.Quit
    End If

    If Not FileSystemObject.FileExists(InputFileName) Then
        Echo "Input File Not Found"
        WScript.Quit
    End If

    If FileSystemObject.GetFile(InputFileName).Size < 1 Then
        Echo "Input File Has No Content"
        WScript.Quit
    End If
End Sub

Tuesday, December 11, 2012

Put a.txt -file to the end of b.txt -file

In a bash script such append.sh file, write the following code: 
#!/bin/bash
A=/root/tmp/test/a.txt
B=/root/tmp/test/b.txt
cat $A >> $B


The >> redirects the output of the cat command (which output file A) to the file B. But instead of overwriting contents of B, it appends to it. If you use a single >, it will instead overwrite any previous content in B.