Link to home
Start Free TrialLog in
Avatar of IndwesAdmin
IndwesAdmin

asked on

Assign count of lines in csv file to variable

I am attempting to assign the line count of a csv file to a variable for use in a Powershell script but so far haven't been able to isolate the actual integer desired.  I would like to have the count assigned to the variable before actually importing the file.  Any suggestions?
Avatar of Enabbar Ocap
Enabbar Ocap
Flag of Italy image

Can you get the line count added to the file on creation?
It's a handy way to tell if you have the complete file and not just some of it.
Otherwise I think you just have to count them.
If you have an idea of the size of a line you could do a rough calculstion based on the file size and yhen adjust when you know yhe actual count.
Sorry about the typos, my phone wont let me edit easily.
Avatar of IndwesAdmin
IndwesAdmin

ASKER

Thanks for the quick response.  Basically, I can look at the file to see how many lines are in it but what I would like to do is add the line count to a variable and display it on my progress bar so is can not only see what record it is working on but how far it is into the process.
ASKER CERTIFIED SOLUTION
Avatar of slavs
slavs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What I have done in the past was check for  a line count number placed as a end of file marker. If this didn't exist then divide the file size by the average expected line length and set the progress bar max to that. Then trap the overflow error on the progress bar in case it ran over and increase the max count by a couple of lines - so it remains at 99%. A user won't notice if it finishes quickly after 90% or so, and they won't notice it 'slowing down' at 99%. A bar is just a guide after all. When the file is complete you have the actual number of lines to use accurately.
Thank you for the input.  I will consider the estimate route but have pretty much decided to manually input the record count when beginning the script since I will always have the file at hand.
It doesn't take very long to open a file just to count the lines, but it can be noticeable and it feels scruffy.
Just suck the whole file in as data and do the line count in memory  to cut down the overhead of keep going back to the disk for the next line.
I was able to solve the problem using the following procedure.

$path = 'C:\Scripts\<FileName>.csv'
$max = ((Get-Content $path).count -1)

(The minus one was to account for the header row in my .csv file.)

Problem solved ... just us the $max variable in the -Status portion of my Write-progress statement.
I've requested that this question be closed as follows:

Accepted answer: 0 points for IndwesAdmin's comment #a39580624

for the following reason:

It works well with my .csv file which is just 0ver 61,000 lines.
You can leave this open as long as you wish.  I am grateful for the input from others and each of the suggestions received.  However, the solution that allowed me to accomplish my intended goal is the one that I posted above to be used by anyone interested.  I have no intention of offending anyone and only attempted to close the ticket as a courtesy to others.
This captured the basic concept of assigning the number of row to a variable and then use the variable in a subsequent statement.  Thanks.