Solved

Append a file using fso.opentextfile command

Posted on 2002-07-25
9
779 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now