Solved

Append a file using fso.opentextfile command

Posted on 2002-07-25
9
791 Views
Last Modified: 2007-12-19
Below is the code for opening a text file to write out the services information.

However, I want to append the details instead of overwriting my text file.
How should I do it? Mode =1 is for reading an input file, mode=2 is for overwriting...

Const Mode= 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
   ("c:\test.txt", Mode , True)
Set colServices =  GetObject("winmgmts:").ExecQuery _
   ("Select * from Win32_Service")
For Each objService in colServices    
   objTextFile.WriteLine(objService.DisplayName & vbTab & _
       objService.State)
Next
objTextFile.Close
0
Comment
Question by:MarcusAu
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:dkaygee
ID: 7179079
Here is an example from MSDN. It looks like the modes are constants "ForReading" and "ForAppending". Look at the FileSystemObject under the Scripting library in the Object Browser and you should see. "TristateFalse" opens the file in ASCII mode.

I modified it just a bit so that the variable f was a TextStream. This Code assumes the file is already created.


Sub OpenTextFileTest
    Dim fs as FileSystemObject
    Dim f as TextStream
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
    f.Write "Hello world!"
    f.Close
End Sub
0
 

Author Comment

by:MarcusAu
ID: 7179195
Yeah , I checked it in Technet too. Forappending value is 2. I even ran the sample code.However, the file is overwritten.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7179323
Actually the syntax for OpenTextFile is:

object.OpenTextFile(filename[, iomode[, create[, format]]])

So in order not to overwrite it should be:
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, False, TristateFalse)

Anthony
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7179324
Or if you just want to keep it simple you can use:
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending)

Anthony
0
 

Author Comment

by:MarcusAu
ID: 7179360
acperkins,
No I am sorry.It's not working following your way. ForAppending is value 2?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 7179401
ForAppending = 8

Anthony
0
 

Author Comment

by:MarcusAu
ID: 7179410
acperkins,
Yes u r right. U deserve the points. :)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7179427
Typically here we give an explanation as to why an "A" was not merited.


Here is the code I was about to post as an example:
1. Start a new project
2. Add a reference to Microsoft Scripting Runtime
3. Add a Command button (Command1)
4. Add the following code

Private Sub Command1_Click()
Const TEST_FILE = "c:\test.txt"
Dim fs As Scripting.FileSystemObject
Dim f As Scripting.TextStream

Set fs = New Scripting.FileSystemObject
If Not fs.FileExists(TEST_FILE) Then
   Set f = fs.CreateTextFile(TEST_FILE)
Else
   Set f = fs.OpenTextFile(TEST_FILE, ForAppending)
End If
f.WriteLine CStr(Now())
f.Close
Set f = Nothing
Set fs = Nothing

End Sub

5. Run it and press the Command button as many times as you wish.  You will notice that it appends to the file.

Anthony
0
 
LVL 1

Expert Comment

by:dkaygee
ID: 7189442
hmmm...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question