Discussion:
Variable to sub doesn't work
(too old to reply)
2***@gmail.com
2015-03-17 12:23:46 UTC
Permalink
Hello,

i have this script:

Sub WriteLogFile(SLogFileLine)
dateStamp = Now()
strFileOutput = "C:\temp\log\status.csv"

Set objFsoLog = CreateObject("Scripting.FileSystemObject")

'Check if file exists
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(cstr(strFileOutput))
End if
'Write Date/Timestamp + Computername + Event in the logfile
logOutput.WriteLine(cstr(dateStamp) + "," + SLogFileLine + ",")

logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing

End Sub

WriteLogFile("sda")

When i call it, it works!
But i want to call it like:

client = "PC123"
WriteLogFile(client)

But when i call it like this, i get only the timestamp + comma. And nothing of the content from the string "client".
What am i doing wrong?
R.Wieser
2015-03-17 12:59:48 UTC
Permalink
Hello 2cholericker (?),
Post by 2***@gmail.com
What am i doing wrong?
I've just executed that script on an XP machine, and it outputs both lines
completely. That means that I've got no idea.
Post by 2***@gmail.com
But when i call it like this, i get only the timestamp + comma.
A single comma, or two (big difference) ?

In the first case it looks like you do not see the whole output. Have you
already tried to write some more data to the logfile to see what the result
is ?

And a warning: You're using the "+" symbol to concatenate (link together)
strings, which might behave different than you expect, as it first tries to
do a mathematical addition. I suggest using the "&" symbol instead.

Hope that helps,
Rudy Wieser


-- Origional message
Post by 2***@gmail.com
Hello,
Sub WriteLogFile(SLogFileLine)
dateStamp = Now()
strFileOutput = "C:\temp\log\status.csv"
Set objFsoLog = CreateObject("Scripting.FileSystemObject")
'Check if file exists
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(cstr(strFileOutput))
End if
'Write Date/Timestamp + Computername + Event in the logfile
logOutput.WriteLine(cstr(dateStamp) + "," + SLogFileLine + ",")
logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing
End Sub
WriteLogFile("sda")
When i call it, it works!
client = "PC123"
WriteLogFile(client)
But when i call it like this, i get only the timestamp + comma. And
nothing of the content from the string "client".
Post by 2***@gmail.com
What am i doing wrong?
Cholericker
2015-03-17 13:27:12 UTC
Permalink
Hello Rudy,

after some trying i have remove the cstr() and now it works for me!
Post by 2***@gmail.com
set logOutput = ObjFsoLog.CreateTextFile(cstr(strFileOutput))
And a warning: You're using the "+" symbol to concatenate (link together)
Thanks for this hint, i have changed it altough.

The complete Code is now:

On Error Resume Next

Sub WriteLogFile(SLogFileLine)
dateStamp = Now()
strFileOutput = "C:\temp\log\status.csv"
wscript.echo strFileOutput

Set objFsoLog = CreateObject("Scripting.FileSystemObject")

'Check if file exists
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
'Write Date/Timestamp + Computername + Event in the logfile
logOutput.WriteLine(cstr(dateStamp) & "," & SLogFileLine & ",")

logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing

End Sub

Call GetAssignedSite

Sub GetAssignedSite

On Error Resume Next

Dim oSMSClient
client = "SVN"


Set oSMSClient = CreateObject ("Microsoft.SMS.Client")

If Err.Number <> 0 Then
WriteLogFile("Could not create SMS Client Object - quitting " & client)
wscript.echo "Fehler"
WriteLogFile(client)
Exit Sub
End If

WriteLogFile("Assigned site is now: " & oSMSClient.GetAssignedSite)
wscript.echo oSMSClient.GetAssignedSite

Set oSMSClient=Nothing
End Sub


Greetings
Dustin
R.Wieser
2015-03-17 14:44:38 UTC
Permalink
Dustin,
Post by Cholericker
after some trying i have remove the cstr() and now it works for me!
Thats good to hear. :-) Odd that it did work for me though ...

By the way, you do not really need to test if the output file exists, and
create it yourself if not. That is actually what the "True" at the end of
the OpenTextFile command is for.

So, this:

----
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
----

can be replaced by this:

----
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
----

Regards,
Rudy Wieser
Post by Cholericker
Hello Rudy,
after some trying i have remove the cstr() and now it works for me!
Post by 2***@gmail.com
set logOutput = ObjFsoLog.CreateTextFile(cstr(strFileOutput))
And a warning: You're using the "+" symbol to concatenate (link together)
Thanks for this hint, i have changed it altough.
On Error Resume Next
Sub WriteLogFile(SLogFileLine)
dateStamp = Now()
strFileOutput = "C:\temp\log\status.csv"
wscript.echo strFileOutput
Set objFsoLog = CreateObject("Scripting.FileSystemObject")
'Check if file exists
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
'Write Date/Timestamp + Computername + Event in the logfile
logOutput.WriteLine(cstr(dateStamp) & "," & SLogFileLine & ",")
logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing
End Sub
Call GetAssignedSite
Sub GetAssignedSite
On Error Resume Next
Dim oSMSClient
client = "SVN"
Set oSMSClient = CreateObject ("Microsoft.SMS.Client")
If Err.Number <> 0 Then
WriteLogFile("Could not create SMS Client Object - quitting " & client)
wscript.echo "Fehler"
WriteLogFile(client)
Exit Sub
End If
WriteLogFile("Assigned site is now: " & oSMSClient.GetAssignedSite)
wscript.echo oSMSClient.GetAssignedSite
Set oSMSClient=Nothing
End Sub
Greetings
Dustin
Cholericker
2015-03-18 08:45:01 UTC
Permalink
Post by R.Wieser
Dustin,
Post by Cholericker
after some trying i have remove the cstr() and now it works for me!
Thats good to hear. :-) Odd that it did work for me though ...
By the way, you do not really need to test if the output file exists, and
create it yourself if not. That is actually what the "True" at the end of
the OpenTextFile command is for.
----
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
----
----
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
----
Regards,
Rudy Wieser
Post by Cholericker
Hello Rudy,
after some trying i have remove the cstr() and now it works for me!
Post by 2***@gmail.com
set logOutput = ObjFsoLog.CreateTextFile(cstr(strFileOutput))
And a warning: You're using the "+" symbol to concatenate (link
together)
Post by Cholericker
Thanks for this hint, i have changed it altough.
On Error Resume Next
Sub WriteLogFile(SLogFileLine)
dateStamp = Now()
strFileOutput = "C:\temp\log\status.csv"
wscript.echo strFileOutput
Set objFsoLog = CreateObject("Scripting.FileSystemObject")
'Check if file exists
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
'Write Date/Timestamp + Computername + Event in the logfile
logOutput.WriteLine(cstr(dateStamp) & "," & SLogFileLine & ",")
logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing
End Sub
Call GetAssignedSite
Sub GetAssignedSite
On Error Resume Next
Dim oSMSClient
client = "SVN"
Set oSMSClient = CreateObject ("Microsoft.SMS.Client")
If Err.Number <> 0 Then
WriteLogFile("Could not create SMS Client Object - quitting " & client)
wscript.echo "Fehler"
WriteLogFile(client)
Exit Sub
End If
WriteLogFile("Assigned site is now: " & oSMSClient.GetAssignedSite)
wscript.echo oSMSClient.GetAssignedSite
Set oSMSClient=Nothing
End Sub
Greetings
Dustin
Hello Rudy,

thanks you for your remarks to advance my script! They helped me very much.

Have a nice day

Dustin
R.Wieser
2015-03-18 08:55:48 UTC
Permalink
Dustin,
Post by Cholericker
thanks you for your remarks to advance my script! They helped me very much.
You're welcome. :-)

Regards,
Rudy Wieser
Post by Cholericker
Post by R.Wieser
Dustin,
Post by Cholericker
after some trying i have remove the cstr() and now it works for me!
Thats good to hear. :-) Odd that it did work for me though ...
By the way, you do not really need to test if the output file exists, and
create it yourself if not. That is actually what the "True" at the end of
the OpenTextFile command is for.
----
if objFsoLog.FileExists(strFileOutput) Then
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
Else
set logOutput = ObjFsoLog.CreateTextFile(strFileOutput)
End if
----
----
Set logOutput = objFsoLog.OpenTextFile(strFileOutput, 8, True)
----
Regards,
Rudy Wieser
Hello Rudy,
thanks you for your remarks to advance my script! They helped me very much.
Have a nice day
Dustin
GS
2015-03-18 00:11:55 UTC
Permalink
These two reusable routines might be handy for you...

Function FSO_ReadTextFile(FileIn)
' Reads an entire file in one step

Dim oFSO, oFile

'On Error GoTo ErrHandler
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile(FileIn, lOpenForR)
FSO_ReadTextFile = oFile.ReadAll

'ErrHandler:
oFile.Close
Set oFSO = Nothing
Set oFile = Nothing
End Function

Sub FSO_WriteTextFile(TextOut, FileOut, ioMode)
' Reusable procedure that Writes or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**

Dim oFSO, oFile, TxtOut

'On Error GoTo ErrHandler
Set oFSO = CreateObject("Scripting.FileSystemObject")
If ioMode = lOpenForA Then '//add to file contents
Set oFile = oFSO.OpenTextFile(FileOut, lOpenForA)
oFile.Write (vbCrLf & TextOut)
Else '//overwrite file contents
Set oFile = oFSO.OpenTextFile(FileOut, lOpenForW)
oFile.Write (TextOut)
End If

'ErrHandler:
oFile.Close
Set oFSO = Nothing
Set oFile = Nothing
End Sub
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2015-03-18 00:50:28 UTC
Permalink
Oops.., I forgot to copy these constants...

Const lOpenR = 1 '//for reading
Const lOpenW = 2 '//for writing
Const lOpenA = 8 '//for appending
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2015-03-18 02:51:23 UTC
Permalink
I forgot I had revised the write routine to create a new file if not
appending...

Sub FSO_WriteTextFile(TextOut, FileOut, ioMode)
' Reusable procedure that Writes or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**

Dim oFSO, oFile, TxtOut

'On Error GoTo ErrHandler
Set oFSO = CreateObject("Scripting.FileSystemObject")
If ioMode = lOpenForA Then '//add to file contents
Set oFile = oFSO.OpenTextFile(FileOut, lOpenForA)
oFile.Write (vbCrLf & TextOut)
Else '//overwrite file contents
Set oFile = oFSO.CreateTextFile(FileOut, lOpenForW)
oFile.Write (TextOut)
End If

'ErrHandler:
oFile.Close
Set oFSO = Nothing
Set oFile = Nothing
End Sub
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Cholericker
2015-03-18 08:53:11 UTC
Permalink
Post by GS
I forgot I had revised the write routine to create a new file if not
appending...
Sub FSO_WriteTextFile(TextOut, FileOut, ioMode)
' Reusable procedure that Writes or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim oFSO, oFile, TxtOut
'On Error GoTo ErrHandler
Set oFSO = CreateObject("Scripting.FileSystemObject")
If ioMode = lOpenForA Then '//add to file contents
Set oFile = oFSO.OpenTextFile(FileOut, lOpenForA)
oFile.Write (vbCrLf & TextOut)
Else '//overwrite file contents
Set oFile = oFSO.CreateTextFile(FileOut, lOpenForW)
oFile.Write (TextOut)
End If
oFile.Close
Set oFSO = Nothing
Set oFile = Nothing
End Sub
--
Garry
Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Hello Garry,

thanks for your script!

Best Regards

Dustin
GS
2015-03-18 13:49:59 UTC
Permalink
Post by Cholericker
Hello Garry,
thanks for your script!
Best Regards
Dustin
You're welcome! Glad to help...
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Loading...