VBScript Error 9 in AtEndOfStream Loop - vbscript

I am using a script like this to loop through some data in a text file:
Do Until objFile.AtEndOfStream
'Check if it is not a blank line; otherwise skip it
If Len(Trim(objFile.ReadLine)) > 0 Then
'Some code
end if
Loop
I print out the values every time through and it gets all the way to end of the file (I can see that it prints every value) but when it reaches the end it always errors out. I used on error resume next and printed out Err.Number and I get error number 9 which is Subscript out of range.
The issue with the file I'm looping through is that the last line is blank. I know this because I've opened the file, removed the final blank line and then run the code and it works fine. This is an automated process though (the file is automatically sent to the FTP and the process runs by an automated task every day) so I can't go in and manually be getting rid of the last line every day. I included that If Len(Trim(objFile.ReadLine)) > 0 Then to take this blank line into account and it works fine on the lines with data but it still throws an error at the end of file.
I could have the loop end when it reaches a blank line but I would like to keep a condition in there in the case that a blank line shows up in the middle of the code for some reason down the line. I figure it'll be cleaner to figure out if it truly is the last line of the file and then end the loop.
Any ideas? Thanks for your help!

First of all, I'd like to ask you how you're actually processing that line you read in since you don't store it anywhere. You read it and pass it to Trim() for checking, but there's no way to get at that line afterwards.
If you have yet another ReadLine within the loop (thinking that's going to be the same line), that's going to be an issue since you might be reading the "blank" line within the if statement. It'll also mean you're throwing away every second line.
It seems to me you should be using something like:
Do Until objFile.AtEndOfStream
thisLine = objFile.ReadLine
If Len(Trim(thisLine)) > 0 Then
' Some code using thisLine, NOT another ReadLine.
End If
Loop
Another thing (once that issue is clarified), are you sure that the final line contains only spaces? I think that's all trim() gets rid of. It may be that there's other white space in there causing you problems, such as TAB, for example.
One way to find out would be to take a copy of an offending file and remove all but the last couple of lines. Then you can insert debug statements into your code to see what's happening:
Do Until objFile.AtEndOfStream
thisLine = objFile.ReadLine
trimLine = Trim(thisLine)
MsgBox ("[" & trimLine & "] " & CStr(Len(trimLine))
If Len(Trim(thisLine)) > 0 Then
MsgBox ("Processing it...")
' Some code using thisLine, NOT another ReadLine.
End If
Loop

Related

How can I convert this VBS script to Bash?

I have this code in VBS that I need to rebuild in Bash. This is a snippet of a larger script that I have to convert. Can someone please help me with this?
I have read a lot of documentation on Bash in the last two days that I have been working on the larger script but am still not sure how to go about doing this. My coworker/trainer essentially handed me this and told me to use Google to learn Bash in order to do this. I feel it's also important to note that I am not familar with VBScript either.
Function Build_Param_Array()
Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set ReadFile = objFSO.OpenTextFile("Param_List.txt")
While Not ReadFile.AtEndOfStream
thisline = ReadFile.ReadLine
Pcount = Pcount + 1
ReDim preserve arrParam(Pcount)
If Not Right(thisline,1) = "|" Then thisline = thisline & "|"
arrParam(Pcount) = thisline
Wend
End Function
Not a vbs guru, but winging it for a general guess, it looks like it reads Param_List.txt, makes sure each line ends with a pipe character, then pushes it onto an array (arrParam).
typeset -a arrParam # declare an array
while read l # read each line from stdin into l
do arrParam+=("${l%|}|") # push the line onto the array, assuring a |
done < Param_List.txt # put the file on the loop's stdin
$l is the line read. ${l%|} is the line read, with any pipe as the last character removed; thus "${l%|}|" explicitly removes a pipe if there was one, then adds one whether or not one was removed.

Stata delimit in command line

I am working on a .do file created by someone else. This person used a semicolon delimiter in the entire file. I am trying to go through this file and see what is going on. I like to do this by selecting a portion of the code and hitting the "Execute Selection (do)" button. However, the delimiter seems to be messing up this. Are there any workarounds for me?
Suppose your do-file looks like this:
#delimit ;
set obs
10 ;
gen x = _n ;
gen y = x^2 ;
gen z = x
^3;
Anytime you highlight a selection and press "Execute selection (do)", Stata creates a temporary, self-contained do-file, with default delimit at cr and runs that:
"When a do-file begins execution, the delimiter is automatically set to
carriage return, even if it was called from another do-file that set the
delimiter to semicolon."
It does not sequentially run those commands from the console. Therefore, if you select the first 2 commands in the do-file above, the temporary do-file includes a call to #delimit whereas if you selected the last 2 commands, the temporary do-file would not have this call and would throw a syntax error for two line commands.
One solution could be to copy-paste selections to a fresh do-file that just had the #delimit command at the beginning, and then run that.
You could also write a script to rid your do-file of semicolons. If a line does not end in a semicolon, then append the next line to the end of the current line, and check this line again. Depending on how complex the syntax is in your do-file, this would be more or less difficult.
Another option is comment out the lines you have already ran by enclosing them with /* */ and to use exit; where you want to stop. You do have to be a little careful with local macros.

Ruby : get output of external command even when there is no line break

I try to run an external command in Ruby, and parse its output .
IO.popen(command, :err=>[:child, :out]) {|ls_io|
ls_io.each do |line|
print line
end
}
This way of doing it works wonders… except when I parse the progress-output of a c-program that shows it progress to stdout with \r.
As long as the c-program has not outputted a \n (that is as long as it has not finished some long-operation), Ruby waits and sees nothing. Then when a \n is outputted, Ruby sees it all
1%\r2%\r3%\r…100%
task finished
I tried all of the many ways to call external commands (eg Calling shell commands from Ruby) ; but none seem to capture the progress. I also tried every opeartor such as STDOUT.sync = true, and the c-program does call fflush(stdout)
I finally found a workaroud. I do :
IO.popen(commande, :err=>[:child, :out]) {|ls_io|
while true
byte=ls_io.read(1)
if byte.nil?
break
end
print byte
end
}
It's stupid… but it works.
Any more elegant way, and much more efficient way to do this ? Performance is terrible, as if the "refresh rate" was slow.
Set the input record separator to "\r" right before your block (provided you know it in advance):
$/ = "\r"
Reference of global preset variables: http://www.zenspider.com/Languages/Ruby/QuickRef.html#pre-defined-variables

Find specific column range and line using VBSCRIPT

I have a txt file that I only need to read the first line, but only want values from column 64-70 from line 1. How do I do this in vbscript? I have looked at several ways to do this but can't get exactly what I'm looking for. Please help.
For the reading characters from the line, use Mid(source_str, 64, 6). -- 6 is the length from character 64 to 70.
As for reading the first line from the text file, you will need to set up a loop to read each line until the end of the file, parse them into an array of strings, then process only the 1st.
Or, since you only need the first line, just run fsoStream.ReadLine() once.
So in your case:
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("filename.txt")
'This only reads the first line of the file.
'To read any others, we would need a loop.
line = file.ReadLine()
thisStr = Mid(line,64,6)

How to delete the first row in the .csv file

I need to delete the first row of the CSV file.
In my script I received CSV file as argument and it's first row contains some unwanted data.
So I need to remove the first row only before processing it.
I'd do something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set csv = fso.OpenTextFile(WScript.Arguments(...))
If Not csv.AtEndOfStream Then csv.SkipLine 'skip first row
Do Until csv.AtEndOfStream
line = csv.ReadLine
'process read line
Loop
csv.Close
Of course you could just as well do a ReadLine without processing the returned value for skipping the first line, as Ekkehard.Horner suggested, but IMO SkipLine better reflects the semantics.
Do a .ReadLine immediately after opening the input file. This will restrict a later line loop -
Do Until .AtEndOfStream
.ReadLine
... process ...
Loop
or a .ReadAll() to the data from the second line onwards only.

Resources