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?
IndwesAdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
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.
0
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
Sorry about the typos, my phone wont let me edit easily.
0
IndwesAdminAuthor Commented:
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.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

slavsCommented:
Is this what you want to do?

#Assign the number of lines to variable
$howmanylines = [System.IO.File]::ReadAllLines("C:\test.txt").Length

#Print to screen
$howmanylines

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
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.
0
IndwesAdminAuthor Commented:
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.
0
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
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.
0
IndwesAdminAuthor Commented:
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.
0
IndwesAdminAuthor Commented:
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.
0
IndwesAdminAuthor Commented:
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.
0
IndwesAdminAuthor Commented:
This captured the basic concept of assigning the number of row to a variable and then use the variable in a subsequent statement.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Miscellaneous

From novice to tech pro — start learning today.