![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | Re: Writing Number to Text File: Type Problem? On Feb 4, 10:58*am, Todd Walton <tdwal...@xxxxxx> wrote: Quote: > FileCount = CountXMLFiles(strPath) 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 Specs![]() |
| | #3 (permalink) |
| | 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. 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 Specs![]() |
| | #4 (permalink) |
| | 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. CountXMLFiles = NumberOfXMLFiles End Function |
My System Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||