top of page

You can use a PowerShell script to count the number of lines in multiple text files saved to a folder.


ree

Enter the file path for the folder after the Get-ChildItem command on the first line. Then specify the extension of the files to be analyzed towards the end of the first line after 'extension - eq'.


Get-ChildItem c:\foofolder\test2 -recurse | where {$_.extension -eq ".txt"} | % {

$_ | Select-Object -Property 'Name', @{

label = 'Lines'; expression = {

($_ | Get-Content).Length

}

}

} | out-file C:\foofolder\test2\lines1.txt

On the last line provide the file path for a new text file to which PowerShell will write the results. Open Windows PowerShell ISE (x86) and then enter the script in a new pane, and then press the play button on the toolbar.


ree

The text file that is generated will list each file name in the source folder and show the number of lines in each in a column to the right.


ree










I ran this script on a set of more than 100,000 text files (which turned out to consist of more than 9 million lines) and it finished the review in less than 30 minutes.


The script can also be used to find the number of lines in other files such as .csv files.


Be sure to enter the file paths in quotes if they include blank spaces.


Thanks to Hari Parkash for posting this script here.


 
 

You can run a simple PowerShell script to get the file size of files saved in various locations on a network.


Get-Childitem -file "C:\foofolder\2022.05.31 Fedex Ground.pdf" | select length

Get-Childitem -file "C:\foofolder\Citation\test\XYZ 000400.pdf" | select length

Get-Childitem -file "U:\O'Shea Documents\personal\port st. lucie.heic" | select length


Simply enter the file path in one column of an Excel spreadsheet, with the Get-Childitem -file command in the column to the left, and the | select length operator in the column to the right. Copy the three columns to NotePad and remove the tabs. Enter the script in Windows PowerShell ISE:


ree

It will output the number of bytes in each file. [1924588 equals 1.9 MB]. You can copy the file sizes back into the Excel spreadsheet so they line up with file paths:



ree

Recently, I used this script to get the length of hundreds of Excel files linked to on a spreadsheet for a trial exhibit list that were saved in dozens of different directories, when requested to find if a file was saved on the list under a different Bates number. The closest in size file proved to be a match.





 
 

I'm working on a trial right now so my posts will have to be short .


Note that if you use a batch file like the one discussed in the Tip of the Night for July 24, 2020, and the names for the renamed files include any of the special characters:

* . " / \ [ ] : ; | , $ + # % ! @

. . . which cannot be used in the Windows file system, the batch file will fail, and files with those characters will not be copied. To get around this problem, use the PowerShell script described in the Tip of the Night for May 5, 2018.



 
 

Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

© 2015 by Sean O'Shea . Proudly created with Wix.com

bottom of page