Solved

Append a file using fso.opentextfile command

Posted on 2002-07-25
9
790 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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