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 - Writing Number to Text File: Type Problem?

Reply
 
Old 02-04-2009   #1 (permalink)
Todd Walton


 
 

Writing Number to Text File: Type Problem?

Sub LogToFile(Message)
Const ForAppending = 8
Set oLogFSO = CreateObject("Scripting.FileSystemObject")
sLogFile = "C:\Logs\log.txt"
Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)

oLogFile.WriteLine(Message)
oLogFile.Close
End Sub

Function CountXMLFiles(strPath)
NumberOfXMLFiles = 0
For Each objFile In objFiles
strCurrentExt = objFSO.GetExtensionName(objFile.Path)
If LCase(strCurrentExt) = "xml" Then
NumberOfXMLFiles = NumberOfXMLFiles + 1
End If
Next
End Function

strPath = "C:\Folder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Set objFiles = objFolder.Files

FileCount = CountXMLFiles(strPath)
LogToFile(FileCount)

-------------------------

When I run the above, it's supposed to count the number of XML files
in a folder and write that number to a text file. What actually
happens is that it creates the text file, but doesn't put anything in
there. Anybody have a guess why? I'm wondering if it has something
to do with FileCount being a number, and LogToFile expecting a
string.

My System SpecsSystem Spec
Old 02-04-2009   #2 (permalink)
Todd Walton


 
 

Re: Writing Number to Text File: Type Problem?

On Feb 4, 10:58*am, Todd Walton <tdwal...@xxxxxx> wrote:
Quote:

> FileCount = CountXMLFiles(strPath)
I changed the above to:
FileCount = CStr(CountXMLFiles(strPath))

and that didn't help. It still just puts a single CR/LF in a blank
text file.

-todd

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


 
 

Re: Writing Number to Text File: Type Problem?

Todd Walton schrieb:
Quote:

> Sub LogToFile(Message)
> Const ForAppending = 8
> Set oLogFSO = CreateObject("Scripting.FileSystemObject")
> sLogFile = "C:\Logs\log.txt"
> Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)
>
> oLogFile.WriteLine(Message)
> oLogFile.Close
> End Sub
>
> Function CountXMLFiles(strPath)
> NumberOfXMLFiles = 0
> For Each objFile In objFiles
> strCurrentExt = objFSO.GetExtensionName(objFile.Path)
> If LCase(strCurrentExt) = "xml" Then
> NumberOfXMLFiles = NumberOfXMLFiles + 1
> End If
> Next
> End Function
>
> strPath = "C:\Folder"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strPath)
> Set objFiles = objFolder.Files
>
> FileCount = CountXMLFiles(strPath)
> LogToFile(FileCount)
>
> -------------------------
>
> When I run the above, it's supposed to count the number of XML files
> in a folder and write that number to a text file. What actually
> happens is that it creates the text file, but doesn't put anything in
> there. Anybody have a guess why? I'm wondering if it has something
> to do with FileCount being a number, and LogToFile expecting a
> string.
If you make your code ready for "Option Explicit" like this:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Sub LogToFile( Message )
Const ForAppending = 8
Dim sLogFile : sLogFile = ".\log.txt"
Dim oLogFile : Set oLogFile = goFS.OpenTextFile( sLogFile, ForAppending, True )
oLogFile.WriteLine Message
oLogFile.Close
End Sub

Function CountXMLFiles( strPath )
NumberOfXMLFiles = 0
Dim objFile
For Each objFile In objFiles
Dim strCurrentExt
strCurrentExt = goFS.GetExtensionName( objFile.Path )
If LCase( strCurrentExt ) = "xml" Then
NumberOfXMLFiles = NumberOfXMLFiles + 1
End If
Next
End Function

Dim strPath : strPath = ".\"
Dim objFolder : Set objFolder = goFS.GetFolder( strPath )
Dim objFiles : Set objFiles = objFolder.Files
Dim FileCount : FileCount = CountXMLFiles( strPath )

LogToFile FileCount

and test it, you'll get a runtime error

Variable ist nicht definiert: 'NumberOfXMLFiles'
[variable NumberOfXMLFiles not defined]

given this message and looking at Function CountXMLFiles( strPath ) it's
obvious that the function name has to be used instead of 'NumberOfXMLFiles'
(assigning to the function names is VBScript's way to return a value
from a function). So change the function to:

Function CountXMLFiles( strPath )
CountXMLFiles = 0
Dim objFile
For Each objFile In objFiles
Dim strCurrentExt
strCurrentExt = goFS.GetExtensionName( objFile.Path )
If LCase( strCurrentExt ) = "xml" Then
CountXMLFiles = CountXMLFiles + 1
End If
Next
End Function

(and think about strPath).
My System SpecsSystem Spec
Old 02-04-2009   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: Writing Number to Text File: Type Problem?


"Todd Walton" <tdwalton@xxxxxx> wrote in message
news:702f04fa-5f59-4f54-85e6-7e71b13628e3@xxxxxx
Quote:

> Sub LogToFile(Message)
> Const ForAppending = 8
> Set oLogFSO = CreateObject("Scripting.FileSystemObject")
> sLogFile = "C:\Logs\log.txt"
> Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)
>
> oLogFile.WriteLine(Message)
> oLogFile.Close
> End Sub
>
> Function CountXMLFiles(strPath)
> NumberOfXMLFiles = 0
> For Each objFile In objFiles
> strCurrentExt = objFSO.GetExtensionName(objFile.Path)
> If LCase(strCurrentExt) = "xml" Then
> NumberOfXMLFiles = NumberOfXMLFiles + 1
> End If
> Next
> End Function
>
> strPath = "C:\Folder"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strPath)
> Set objFiles = objFolder.Files
>
> FileCount = CountXMLFiles(strPath)
> LogToFile(FileCount)
>
> -------------------------
>
> When I run the above, it's supposed to count the number of XML files
> in a folder and write that number to a text file. What actually
> happens is that it creates the text file, but doesn't put anything in
> there. Anybody have a guess why? I'm wondering if it has something
> to do with FileCount being a number, and LogToFile expecting a
> string.
You forgot to assign an exit value to the CountXMLFiles function:
CountXMLFiles = NumberOfXMLFiles
End Function


My System SpecsSystem Spec
Old 02-04-2009   #5 (permalink)
Paul Randall


 
 

Re: Writing Number to Text File: Type Problem?

How about adding a MsgBox to display what is in the variable Message at the
beginning of your LogToFile subroutine. Also, while testing, you may want
to delete your log file at the beginning of your script so you know it is
starting out fresh.

The scripting help file, Script56.chm, has a lot of info about the system
object, but it only talks about 'text' files, text streams, etc., without
defining what that 'text' means. If your message variable contains only
printable characters and carriage returns and line feeds, 'text' should not
be a problem.

-Paul Randall

"Todd Walton" <tdwalton@xxxxxx> wrote in message
news:702f04fa-5f59-4f54-85e6-7e71b13628e3@xxxxxx
Quote:

> Sub LogToFile(Message)
> Const ForAppending = 8
> Set oLogFSO = CreateObject("Scripting.FileSystemObject")
> sLogFile = "C:\Logs\log.txt"
> Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)
>
> oLogFile.WriteLine(Message)
> oLogFile.Close
> End Sub
>
> Function CountXMLFiles(strPath)
> NumberOfXMLFiles = 0
> For Each objFile In objFiles
> strCurrentExt = objFSO.GetExtensionName(objFile.Path)
> If LCase(strCurrentExt) = "xml" Then
> NumberOfXMLFiles = NumberOfXMLFiles + 1
> End If
> Next
> End Function
>
> strPath = "C:\Folder"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strPath)
> Set objFiles = objFolder.Files
>
> FileCount = CountXMLFiles(strPath)
> LogToFile(FileCount)
>
> -------------------------
>
> When I run the above, it's supposed to count the number of XML files
> in a folder and write that number to a text file. What actually
> happens is that it creates the text file, but doesn't put anything in
> there. Anybody have a guess why? I'm wondering if it has something
> to do with FileCount being a number, and LogToFile expecting a
> string.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Writing information to text file VB Script
Problem Writing file to CD .NET General
get the number of lines in a text file PowerShell
Newbie - Writing to host and a text file PowerShell


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