Appending Text Files 
Author Message
 Appending Text Files

I am relatively new to the Scripting world and I have a question that I hope
is easily answered.  I am writing a script that includes a backup routine.
In the backup routine I am replacing several files, but I also need to
append a text file to an existing file. (To keep a running total of all
transactions)  What is the best/easiest way to do this in WSF.

Thanks in advance for your help.

John Baker



Wed, 23 Jul 2003 01:12:30 GMT  
 Appending Text Files
Use a Scripting.FileSystemObject instance.  The OpenTextFile method can open text files
ForAppending.

OpenTextFile Method
http://msdn.microsoft.com/scripting/VBScript/doc/vsmthOpenTextFile.htm

The EXE links below are downloads of the complete documentation sets in HTMLHelp format - highly
recommended for offline use.  The Tutorial and "...User's Guide" content is also included in the
downloaded documentation...

WSH 2.0 Tutorial
http://msdn.microsoft.com/scripting/windowshost/doc/wsTutorialTOC.htm
WSH Documentation
http://msdn.microsoft.com/scripting/windowshost/docs/reference/defaul...
http://msdn.microsoft.com/scripting/windowshost/wshdoc.exe

VBScript User's Guide
http://msdn.microsoft.com/scripting/vbscript/doc/vbstutor.htm
VBScript Documentation
http://msdn.microsoft.com/scripting/vbscript/techinfo/vbsdocs.htm
http://msdn.microsoft.com/scripting/vbscript/download/vbsdoc.exe

FileSystemObject User's Guide
http://msdn.microsoft.com/scripting/vbscript/doc/jsFSOTutor.htm
VBScript Run-Time Library Reference [FileSystemObject/Dictionary]
http://msdn.microsoft.com/scripting/vbscript/doc/VBSFSOTOC.htm

JScript User's Guide
http://msdn.microsoft.com/scripting/jscript/doc/jsconJScriptUsersGuid...
JScript Documentation
http://msdn.microsoft.com/scripting/jscript/techinfo/jsdocs.htm
http://msdn.microsoft.com/scripting/jscript/download/jsdoc.exe

WSC Tutorial
http://msdn.microsoft.com/scripting/scriptlets/doc/lettitle.htm
WSC Documentation
http://msdn.microsoft.com/scripting/scriptlets/serverdocs.htm
http://msdn.microsoft.com/scripting/scriptlets/wscdoc.exe

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> I am relatively new to the Scripting world and I have a question that I hope
> is easily answered.  I am writing a script that includes a backup routine.
> In the backup routine I am replacing several files, but I also need to
> append a text file to an existing file. (To keep a running total of all
> transactions)  What is the best/easiest way to do this in WSF.

> Thanks in advance for your help.

> John Baker




Wed, 23 Jul 2003 08:22:10 GMT  
 Appending Text Files
This Subroutine might help.

You have to read all the text from the text file, assign it to a variable
(sText in the subroutine), and pass it to the subroutine. Just specify the
name of the final file.

'********************************************************************
'* Sub AppendToFile(sText, sFilespec)
'* Purpose: Appends text to a file, if the file doesn't exist it
'*   creates it
'* Input:   sText     Quoted text, text returned by a function,
'*      or any kind of generated Ascii text
'*   sFilespec   A name and path for the text file
'* Usage:
'*     AppendToFile "Complete " & Date, "c:\foldername\logfile.log"
'********************************************************************
Sub AppendToFile(sText, sFilespec)
 Const ForReading = 1, ForWriting = 2, ForAppending = 8
 Dim oFso, oTextStream
 Set oFso = CreateObject("Scripting.FileSystemObject")
 Set oTextStream = oFso.OpenTextFile(sFilespec, ForAppending, True)
 oTextStream.Write(sText & vbcrlf)
 oTextStream.Close
End Sub


Quote:
> I am relatively new to the Scripting world and I have a question that I
hope
> is easily answered.  I am writing a script that includes a backup routine.
> In the backup routine I am replacing several files, but I also need to
> append a text file to an existing file. (To keep a running total of all
> transactions)  What is the best/easiest way to do this in WSF.

> Thanks in advance for your help.

> John Baker




Wed, 23 Jul 2003 14:41:35 GMT  
 Appending Text Files
This is pretty clean code for appending the text contents of one file to
another. It uses the Script Host to call the command shell. (You can do it
with VBScript and the OpenTextFile method but it's a lot more code.)

Just substitute the path and filenames for your log files in the sCommandStr
variable assignment statement.

Option Explicit

Dim sCommandStr
sCommandStr = "type c:\sourcefile.txt>>targetfile2.txt"
RunCMD sCommandStr

Sub RunCMD(sCommandStr)
 Dim oShell
 Set oShell = Wscript.CreateObject("Wscript.Shell")
 oShell.Run "cmd /c " & sCommandStr, 0
End Sub

Quote:
> I am relatively new to the Scripting world and I have a question that I
hope
> is easily answered.  I am writing a script that includes a backup routine.
> In the backup routine I am replacing several files, but I also need to
> append a text file to an existing file. (To keep a running total of all
> transactions)  What is the best/easiest way to do this in WSF.

> Thanks in advance for your help.

> John Baker




Wed, 23 Jul 2003 14:42:22 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. appending text files with vbscript

2. appending text to output text file

3. How do i append info to a text file

4. FSO - No text in append file

5. Append to text file

6. append dos output text to log file

7. Help appending to a text file using vbscript

8. Appending to a text file

9. Appending to text file?

10. Append a text file

11. Appending 2 text files

12. Creating and or Appending to a text file

 

 
Powered by phpBB® Forum Software