How to delete the first row in the .csv file - vbscript

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.

Related

How to remove invisible new line characters in Perl

I am writing a shell script in perl that takes values from two databases and compares them. When the script is finished it outputs a report that is supposed to be formatted this way:
Table Name Date value from Database 1 value from Database 2 Difference
The output is printed into a report file, but even when it is output to the command console it looks like this:
tablename 2017-06-20 7629628
7629628
0
Here's my code that makes the string then outputs it to the file:
$outputstring="$tablelist[0] $DATErowcount0[$output_iteration] $rowcount0[$output_iteration] $TDrowcount0[$output_iteration] $count_dif\n";
print FILE $outputstring;
There seems to be a newline character hidden after $rowcount0[$output_iteration] and before $count_dif. What do I need to do to fix this/print it all in one line?
To fill the arrays with values, values are read from files created by SQL commands.
Here's some of the code:
`$num_from_TDfile=substr $r2, 16;
$date_from_TDfile = substr $r2, 0, 12;
$TDrowcount0[$TDnum_rows0]=$num_from_TDfile;
$DATETDrowcount0[$TDnum_rows0]=$date_from_TDfile;
$TDnum_rows0=$TDnum_rows0+1;`
Adding the chomp to each of the strings taken from the files as suggested by tadman fixed the output so that it was all on one line rather than three lines as in the question's example.

writing in specific position in a text file using ruby

I want to insert data in specific positions in a text file, like in line 1 starting from position 10, how can I do it using ruby?
I also want to pass fake data into this file using fakker gem or in any other way possible. Like sending phone number, name, SSN etc.
Here's a sample script that takes two arguments and writes a modified copy of the first file's contents to the second file:
require 'faker'
input = File.open(ARGV[0], 'r')
lines = input.readlines
lines[0].gsub!(/^(.{0,10})/, '\1' + Faker::Base.numerify('###').to_s)
output = File.open(ARGV[1], 'w')
lines.each do |line|
output.write(line)
end
If you have an input file that looks like:
12345678901234567890
^^^ fake data
the output might look like:
12345678909451234567890
^^^ fake data
Since I opened the output file after reading the input file, you can pass the same file name as both the first and the second argument. That isn't exactly inserting the string into the file, but it's as close as you'll get.
The key line is:
lines[0].gsub!(/^(.{0,10})/, '\1' + Faker::Base.numerify('###').to_s)
It takes the fist line and substitutes in place a random 3-digit integer. If there are fewer than 10 characters in the first line, it'll append the random data to the end of the line. If you'd prefer to not substitute, you might want to remove the beginning of the range in the regex:
/^(.{10})/
Or maybe do something else if lines[0].length < 10.

VBScript Error 9 in AtEndOfStream Loop

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

Inserting new line when joining files in VBScript

I have two text files that I want to combine ..I am using the below code to do that ..the issue is at the start of the second file this code is inserting some weird characters like spaces..Is there a way to insert a new line instead of using writeline.
Set txsOutput = FSO.CreateTextFile(strOutputPath)
Set txsInput = FSO.OpenTextFile(strInputPath,1)
txsOutput.Writeline txsInput.ReadAll
Thanks
.ReadAll() reads the trailing EOL(s) of the file. .Writeline will add a further EOL. Use .Write instead to get an exact copy of the first input file as the head of the output file.
If the "weird characters like spaces" are - unwanted - parts of the first file, you'll have to use string ops (Instr, Left, Replace, ...) or a RegExp to clean the data.
If they come from the second file (assuming you used .ReadAll for that too), you should check the encoding of that file and/or clean the data using the methods above.

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)

Resources