Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Need help with log ( text ) file - write vs append

Reply
 
Old 02-07-2009   #1 (permalink)
Tcs


 
 

Need help with log ( text ) file - write vs append

The code below os from a larger script. It gets info regarding the
swap file, and determines if it can change the min/max values. All of
that is working just fine.

Before I finish, I want to log what the script has done. This is my
problem.

If the log file ( .csv ) doesn't exist, I need to create it, and write
the header line for Excel. Then I need to write the log entry of the
machine we've just dealt with.

If the log file does exist, don't create the file and wipe out the
previous entries, just append.

It's probably something simple, although I don't know. While I'm not
a newbie at vbscript, I'm not exactly a guru either.

If anyone could suggest a fix, I would appreciate it.

Thanks in advance.

-----

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.OpenTextFile(strOutPutFile, ForWriting, True)


WScript.Echo _
"Made it to 'Log it..."


WScript.Echo _
"PrintFile...(" & strOutPutFile & ")"


If objFSO.FileExists(strOutPutFile) = False Then

WScript.Echo _
"output file doesn't exist..."


Const ForWriting = 1


Set objFile = objFSO.CreateTextFile(strOutPutFile, ForWriting,
True)


' Write the header for Excel


objFile.WriteLine _
Chr(34) & "Computer" & Chr(34) & Chr(44) & _
Chr(34) & "SkipItFlag" & Chr(34) & Chr(44) & _
Chr(34) & "OldMinPageSize MB" & Chr(34) & Chr(44) & _
Chr(34) & "OldMaxPageSize MB" & Chr(34) & Chr(44) & _
Chr(34) & "InstalledRAM MB" & Chr(34) & Chr(44) & _
Chr(34) & "NewMinPageSize MB" & Chr(34) & Chr(44) & _
Chr(34) & "NewMaxPageSize MB" & Chr(34) & Chr(44) & _
Chr(34) & "DiskSpaceFree MB" & Chr(34) & Chr(44) & _
Chr(34) & "Date" & Chr(34) & Chr(44) & _
Chr(34) & "Time" & Chr(34) & Chr(44) & _
Chr(34) & "Notes" & Chr(34)


objFile.Close

End If


Const ForAppending = 8


Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending, True)


objFile.WriteLine _
Chr(34) & WshNetwork.ComputerName & Chr(34) & Chr(44) & _
Chr(34) & intSkipItFlag & Chr(34) & Chr(44) & _
Chr(34) & intOldMinPageSize & Chr(34) & Chr(44) & _
Chr(34) & intOldMaxPageSize & Chr(34) & Chr(44) & _
Chr(34) & intInstalledRAM & Chr(34) & Chr(44) & _
Chr(34) & intNewMinPageSize & Chr(34) & Chr(44) & _
Chr(34) & intNewMaxPageSize & Chr(34) & Chr(44) & _
Chr(34) & intDiskSpaceFree & Chr(34) & Chr(44) & _
Chr(34) & strDate & Chr(34) & Chr(44) & _
Chr(34) & strTime & Chr(34) & Chr(44) & _
Chr(34) & strNotes & Chr(34)


' Clean up


objFile.Close

'
' End of script
'

My System SpecsSystem Spec
Old 02-07-2009   #2 (permalink)
Paul Randall


 
 

Re: Need help with log ( text ) file - write vs append


"Tcs" <someone@xxxxxx> wrote in message
news7cso4l9sqtvq6l67otij7fh489kt9mrb4@xxxxxx
Quote:

> The code below os from a larger script. It gets info regarding the
> swap file, and determines if it can change the min/max values. All of
> that is working just fine.
>
> Before I finish, I want to log what the script has done. This is my
> problem.
>
> If the log file ( .csv ) doesn't exist, I need to create it, and write
> the header line for Excel. Then I need to write the log entry of the
> machine we've just dealt with.
>
> If the log file does exist, don't create the file and wipe out the
> previous entries, just append.
>
> It's probably something simple, although I don't know. While I'm not
> a newbie at vbscript, I'm not exactly a guru either.
>
> If anyone could suggest a fix, I would appreciate it.
>
> Thanks in advance.
>
> -----
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> 'Set objFile = objFSO.OpenTextFile(strOutPutFile, ForWriting, True)
>
>
> WScript.Echo _
> "Made it to 'Log it..."
>
>
> WScript.Echo _
> "PrintFile...(" & strOutPutFile & ")"
>
>
> If objFSO.FileExists(strOutPutFile) = False Then
>
> WScript.Echo _
> "output file doesn't exist..."
>
>
> Const ForWriting = 1
>
>
> Set objFile = objFSO.CreateTextFile(strOutPutFile, ForWriting,
> True)
>
>
> ' Write the header for Excel
>
>
> objFile.WriteLine _
> Chr(34) & "Computer" & Chr(34) & Chr(44) & _
> Chr(34) & "SkipItFlag" & Chr(34) & Chr(44) & _
> Chr(34) & "OldMinPageSize MB" & Chr(34) & Chr(44) & _
> Chr(34) & "OldMaxPageSize MB" & Chr(34) & Chr(44) & _
> Chr(34) & "InstalledRAM MB" & Chr(34) & Chr(44) & _
> Chr(34) & "NewMinPageSize MB" & Chr(34) & Chr(44) & _
> Chr(34) & "NewMaxPageSize MB" & Chr(34) & Chr(44) & _
> Chr(34) & "DiskSpaceFree MB" & Chr(34) & Chr(44) & _
> Chr(34) & "Date" & Chr(34) & Chr(44) & _
> Chr(34) & "Time" & Chr(34) & Chr(44) & _
> Chr(34) & "Notes" & Chr(34)
>
>
> objFile.Close
>
> End If
>
>
> Const ForAppending = 8
>
>
> Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending, True)
>
>
> objFile.WriteLine _
> Chr(34) & WshNetwork.ComputerName & Chr(34) & Chr(44) & _
> Chr(34) & intSkipItFlag & Chr(34) & Chr(44) & _
> Chr(34) & intOldMinPageSize & Chr(34) & Chr(44) & _
> Chr(34) & intOldMaxPageSize & Chr(34) & Chr(44) & _
> Chr(34) & intInstalledRAM & Chr(34) & Chr(44) & _
> Chr(34) & intNewMinPageSize & Chr(34) & Chr(44) & _
> Chr(34) & intNewMaxPageSize & Chr(34) & Chr(44) & _
> Chr(34) & intDiskSpaceFree & Chr(34) & Chr(44) & _
> Chr(34) & strDate & Chr(34) & Chr(44) & _
> Chr(34) & strTime & Chr(34) & Chr(44) & _
> Chr(34) & strNotes & Chr(34)
>
>
> ' Clean up
>
>
> objFile.Close
>
> '
> ' End of script
> '
You should be able to adapt the following snippet. Look at OpenTextFile
method in the scripting help file, script56.chm.

With CreateObject("Scripting.FileSystemObject")._
OpenTextFile(sFilePath, 8, true)
.Write sData
.Close
End With



My System SpecsSystem Spec
Old 02-08-2009   #3 (permalink)
ekkehard.horner


 
 

Re: Need help with log ( text ) file - write vs append

Tcs schrieb:
Quote:

> The code below os from a larger script. It gets info regarding the
> swap file, and determines if it can change the min/max values. All of
> that is working just fine.
>
> Before I finish, I want to log what the script has done. This is my
> problem.
>
> If the log file ( .csv ) doesn't exist, I need to create it, and write
> the header line for Excel. Then I need to write the log entry of the
> machine we've just dealt with.
>
> If the log file does exist, don't create the file and wipe out the
> previous entries, just append.
[...]
You need to create or open (append) the file depending on its
existence:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\tcslog.txt"

' start afresh
If oFS.FileExists( sFSpec ) Then oFS.DeleteFile sFSpec

Dim nMsg
For nMsg = 1 To 3
writeLog oFS, sFSpec, "message #" & nMsg
Next
WScript.Echo oFS.OpenTextFile( sFSpec ).ReadAll()

Sub writeLog( oFS, sFSpec, sMsg )
Const ForAppending = 8
Dim oTS
If oFS.FileExists( sFSpec ) Then
Set oTS = oFS.OpenTextFile( sFSpec, ForAppending )
Else
Set oTS = oFS.CreateTextFile( sFSpec, True )
oTS.WriteLine "TimeStamp,Message"
End If
oTS.WriteLine Now & ",""" & sMsg & """"
oTS.Close
End Sub

output:
(If oFS.FileExists( sFSpec ) Then oFS.DeleteFile sFSpec active)

TimeStamp,Message
08.02.2009 19:19:16,"message #1"
08.02.2009 19:19:16,"message #2"
08.02.2009 19:19:16,"message #3"

output:
(If oFS.FileExists( sFSpec ) Then oFS.DeleteFile sFSpec disabled)

TimeStamp,Message
08.02.2009 19:19:16,"message #1"
08.02.2009 19:19:16,"message #2"
08.02.2009 19:19:16,"message #3"
08.02.2009 19:19:53,"message #1"
08.02.2009 19:19:53,"message #2"
08.02.2009 19:19:53,"message #3"
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
can't write to text file after installing app Vista General
Append text to top of text file? PowerShell
VB Script that searches a file for different text, with an append. VB Script
unknown error when trying to write to text file VB Script
How to read and write a text file? VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46