Solved

Append a file using fso.opentextfile command

Posted on 2002-07-25
9
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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