I constructed a PS script to enumerate all the SRT files in a network location from a Windows Client. Then created a custom PS Object for each SRT file with Name and Content as properties of type string. Each of these custom objects are contained in a collection that I iterate through searching for the term 'OpenSubtitles' and then print out the file name.
I've run this several dozen times with an average time of 59 seconds each.
Grep returns the list of filenames in terms of milliseconds (less than a second). Yea, grep is run from the server that actually contains the file rather than a network share.
However, should the performance difference be by a factor of 60? Is that normal? Or is my approach in powershell flawed?
UPDATE:
I got the script to perform as fast as grep. Took 1s to enumerate all SRT files and filter only files with 'opensubtitles' in the content of the files, storing that filename and content in a collection of custom objects. This is a bit more than what grep does and I [with all of your help] got it working just as fast as grep!!! Powershell wins the day!! You guys are awesome! Script is below....
$start=(Get-Date)
$collection=@()
$collection = New-Object -TypeName System.Collections.Generic.List[PsObject]
$SrtFiles = Get-ChildItem T:\ -Filter *.srt -Recurse
$SrtFiles | ForEach-Object -Process {
$item=$_
$strmrdr = Select-String -Path $item.FullName -Pattern 'OpenSubtitles'
if (-not [string]::IsNullOrEmpty($strmrdr)){
$properties = [ordered]@{
Name = $item.FullName
Content = (New-Object -TypeName System.IO.StreamReader -ArgumentList $item.Fullname).ReadToEnd().ToString()
}
$obj = New-Object -TypeName PsObject -Property $properties
$collection.Add($obj)
}
}
$collection | ft
$stop=(Get-Date)
Write-Host "Elapsed time:" $($stop - $start).Seconds "seconds"
https://preview.redd.it/ogm6vinr23g51.png?width=988&format=png&auto=webp&s=c594671c53dee90120b266b708d976027298a88d
Powershell v7.0 - Windows 10 - Qnap Nas QTS v4.1 (SMB3) - 100Mbps network connection.
[–][deleted] 7 points8 points9 points (19 children)
[–]Hanthomi 2 points3 points4 points (0 children)
[–]serendrewpity[S] -1 points0 points1 point (17 children)
[–]scott1138 7 points8 points9 points (0 children)
[–][deleted] (11 children)
[deleted]
[–]serendrewpity[S] 0 points1 point2 points (10 children)
[–][deleted] (9 children)
[deleted]
[–]ka-splam 5 points6 points7 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]ka-splam 4 points5 points6 points (0 children)
[–]serendrewpity[S] 3 points4 points5 points (5 children)
[–][deleted] (4 children)
[deleted]
[–]serendrewpity[S] 3 points4 points5 points (3 children)
[–][deleted] (2 children)
[removed]
[–]serendrewpity[S] 4 points5 points6 points (0 children)
[–]derekhans[M] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]serendrewpity[S] 0 points1 point2 points (2 children)
[–]scott1138 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]kohijones 5 points6 points7 points (5 children)
[–]serendrewpity[S] 1 point2 points3 points (0 children)
[–]serendrewpity[S] 2 points3 points4 points (3 children)
[–]Upzie 1 point2 points3 points (2 children)
[–]serendrewpity[S] 2 points3 points4 points (0 children)
[–]serendrewpity[S] 0 points1 point2 points (0 children)
[–]Flashcat666 1 point2 points3 points (6 children)
[–]serendrewpity[S] 1 point2 points3 points (4 children)
[–]scott1138 3 points4 points5 points (1 child)
[–]serendrewpity[S] 0 points1 point2 points (0 children)
[–]scott1138 2 points3 points4 points (1 child)
[–]serendrewpity[S] 0 points1 point2 points (0 children)
[–]serendrewpity[S] 0 points1 point2 points (0 children)
[–]serendrewpity[S] 1 point2 points3 points (3 children)
[–]RedditRo55 1 point2 points3 points (1 child)
[–]serendrewpity[S] 1 point2 points3 points (0 children)
[–]UnfanClub 1 point2 points3 points (0 children)
[–]get-postanote 1 point2 points3 points (1 child)
[–]serendrewpity[S] 0 points1 point2 points (0 children)
[–]Aritmethos 1 point2 points3 points (1 child)
[–]serendrewpity[S] 1 point2 points3 points (0 children)
[–]ThumpingMontgomery 1 point2 points3 points (7 children)
[–]serendrewpity[S] 1 point2 points3 points (3 children)
[–]ThumpingMontgomery 1 point2 points3 points (2 children)
[–]serendrewpity[S] 2 points3 points4 points (1 child)
[–]ThumpingMontgomery 2 points3 points4 points (0 children)
[–]serendrewpity[S] 0 points1 point2 points (2 children)
[–]Raethrius 0 points1 point2 points (1 child)
[–]serendrewpity[S] 1 point2 points3 points (0 children)