Nilpo`s Scripting Secrets, Vol I - More scripting secrets
(Page 4 of 4 )
Reading the last line of a text file
I recently saw a very long discussion in which a half a dozen or more people were suggesting different ways of reading only the last line of a text file. Some suggested splitting the file into an array at lone breaks, others suggested parsing by other means. My point is that this was a classic example of over thinking a problem.
Const ForReading = 1
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("C:file.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Loop
First off, parsing a text file in VBScript requires reading the entire file into memory. That is far too inefficient right from the get-go. All you need to do is open the text file with the FileSystemObject and create a loop that reads the file line by line. Because the value of strLine is reassigned each time the loop iterates, it only ever contains one line. When the loop exits, strLine contains the last line that was read, which incidentally happens to be the last line of the file.
Playing system sounds
I was recently asked on my web site if there was any way to play system sounds in a script. As it turns out, there sure is.
PlaySound
Sub PlaySound
strCommand = "sndrec32 /play /close " & "%WINDIR%Mediatada.wav"
CreateObject("WScript.Shell").Run strCommand, 0, False
End Sub
Windows system sounds are .wav files that can be found in the Media folder in the Windows directory. They can be played through sndrec32.exe from the command line without any extra windows.
Well, that wraps up volume one of Nilpo’s Scripting Secrets. Stick around for the next installment, when I will reveal even more useful scripting techniques. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |