Well, since you wanted to dive into VBScript, here is a couple of things to help you out.

First of all, instead of using the CreateTextFile, which I personally found not too reliable, use the OpenTextFile method instead.

The OpenTextFile takes basically three parameters. That is:
    1. The filename;
    2. The file mode;
and,
3. A boolean variable, telling whether the file should be created if it does not yet exist.

A 4th parameter, will determine if this is a unicoded file or not. So, go ahead with a line like this:

Set LogFile = LogFSO.OpenTextFile( "c:\VSScriptLog.txt", 8, True)

Notice, please, that our second parameter here is set to the value of 8. This is the Append value of the File Mode. Here are the mode values of interest:
    1 = Reading,
    2 = Writing,
    8 = Appending.

Since our third parameter is set to True, it means that the file will be created, if it does not already exist. And what if it does exist? Well, then it will be appended by your next line:

LogFile.WriteLine "Here goes my text"

You now have added a line to the bottom of your text file, and are ready to close it all up. Do this with the simple line:

LogFile.Close

Good scripting habit, even if this is told not to be necessary in all cases, is to clear out the objects once we are done with them. For one thing, to free up memory. Your script made use of two objects, so we clear them both out, with the two following lines. We also want to make sure, the script does not through any errors on us, should the objects not happen to be in operation, so we do a quick check to see if either of them currently is defined.

If Not LogFSO Is Nothing Then    Set LogFSO = Nothing
If Not LogFile Is Nothing Then    Set LogFile = Nothing

Your script now has performed the job you were asking, and your code could end here. That is really all there is to the basic file handling in VBScript.

I also wanted to make you aware of one extra detail. In a VBScript, where you are going to use the newly defined variable immediately, you could ease your code for human readibility slightly. Here are two lines, that shows what I am trying to say. Notice that each line first cares for the definition - or making ready - of the variable itself. And then, secondly it puts the variable into action.

Dim LogFSO: Set LogFSO = CreateObject( "FileSystemObject")
Dim LogFile: Set LogFile = LogFSO.OpenTextFile( "c:\VSScriptLog.txt", 8, True)

And, if your code wants to be very much more readable and transparent, you could always start it out by setting the File Mode constants up:

Const ForReading = 1
Const ForWriting = 2
Const ForAppend = 8


Now, Let's pull it altogether, and your full script would look like this:

' Sample Script:
' Set up the File Mode Values
Const ForAppend = 8

' Open the objects
Dim LogFSO: Set LogFSO = CreateObject( "FileSystemObject")
Dim LogFile: Set LogFile = LogFSO.OpenTextFile( "c:\VSScriptLog.txt", ForAppend, True)

' Append a line to the text file
LogFile.WriteLine "Here goes my text."

' Close the file when we are done
LogFile.Close

' Clear out any objects that are no longer in use
If Not LogFSO Is Nothing Then    Set LogFSO = Nothing
If Not LogFile Is Nothing Then    Set LogFile = Nothing
'---End Of Code---

That should do your job. Once you get the grasp of it, it ain't really that complicated. Mind you, setting the file mode to the wrong value, could have fatal consequences. Further error handling would be needed to prevent such faults. But here you have the basics of it all.


David

On 3/19/2015 10:34 AM, Rick Thomas via Scripting wrote:
> I want to create a method to append lines to a text file:
>
> The first code I pulled from google:
>
> To create a simple log file for debugging Aarons script:
>
> DIM logFSO, logFile
>
> Set logFSO = CreateObject("Scripting.FileSystemObject")
>
> Set logFile = logFSO.CreateTextFile( "c:\VSScriptLog.txt", True)
>
> [Above just goes high in the script to be executed once when loaded]
>
> logFile.WriteLine("Logged ok")
>
> [Above goes wherever i want to print a line to the log]
>
> logFile.Close
>
>  [Where do I put the close for the file in a script so it closes when the
> script is done]
>
> Or with the file options can i just have a method:
>
> High in code:
>
> DIM logFSO, logFile
>
> Set logFSO = CreateObject("Scripting.FileSystemObject")
>
> Then:
>
> in a print Sub:
>
> Set logFile = logFSO.CreateTextFile( "c:\VSScriptLog.txt", True)
>
> logFile.WriteLine("Logged ok")
>
> logFile.Close
>
> End Sub
>
> I want to just append lines to a text file upon a WriteLine method of the
> fso object for debugging.
>
> Rick USA
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.window-eyes.com/private.cgi/scripting-window-eyes.com/attachments/20150319/530eede4/attachment.htm>
> _______________________________________________
> Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Ai Squared.
>
> For membership options, visit http://lists.window-eyes.com/options.cgi/scripting-window-eyes.com/trailerdavid%40hotmail.com. > For subscription options, visit http://lists.window-eyes.com/listinfo.cgi/scripting-window-eyes.com > List archives can be found at http://lists.window-eyes.com/private.cgi/scripting-window-eyes.com
>
>

_______________________________________________
Any views or opinions presented in this email are solely those of the author 
and do not necessarily represent those of Ai Squared.

For membership options, visit 
http://lists.window-eyes.com/options.cgi/scripting-window-eyes.com/archive%40mail-archive.com.
For subscription options, visit 
http://lists.window-eyes.com/listinfo.cgi/scripting-window-eyes.com
List archives can be found at 
http://lists.window-eyes.com/private.cgi/scripting-window-eyes.com

Reply via email to