Solved

Append a file using fso.opentextfile command

Posted on 2002-07-25
9
783 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

930 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

11 Experts available now in Live!

Get 1:1 Help Now