How to Loop Through List of Filepaths to Find Specific String within Files

Viewed 43

Edited for clarity

Apologies if this has been answered elsewhere. I'm new to Powershell and have tried searching extensively, but for some reason I keep running in circles so any help is appreciated.

I have a txt file containing the filepaths of a series of files. What I need to do is, for a given filepath, search the contents of the file at that path for a string containing a changelog entry and extract from that a substring containing the software version at the time that file was saved.

I was able to search through one file explicitly named (rather than from a path in a txt file) and get the full string using this:

Select-String ": Saved - V" .\samplefilepath.acd | out-file .\VersionFinderResults.txt

This gets me a string that looks something like this:

Tests\samplefilepath.ACD:7:2018-02-08 16:37:38.987: Saved - V20.01.00/3489.000

Once I have the full strings, I can use this code to output the substrings to another .txt file:

 foreach($line in [System.IO.File]::ReadLines(".\VersionFinderResults.txt"))
{
    $sub = $line.Substring($line.length-17,8)| Out-File -FilePath .\VersionFinderResults2.txt -append
    
}

This gets me the desired substring like this:

20.01.00

Convoluted, I know, but I'm learning. :) These two commands get me every instance of the substring I need, appended one per line in VersionFinderResults2.txt just as I need them. Now I need to bundle it all up in what I assume would be a for loop, looking through a txt file of filepaths and running the above lines on the file at each path in that list. If anyone could point me in the right direction, I would be most grateful. TIA

1 Answers

It seems like this is asking close to the same thing as this other question I answered the other day

Putting in the brief answer here.

There's a module I write for RegEx called Irregular.

In that, there's a regex called ?<Code_SemanticVersion>.

You might be able to:

Install-Module Irregular -Scope CurrentUser
Import-Module Irregular -PassThru
dir $DirectoryWithYourFiles -Recurse | ?<Code_SemanticVersion>

See the other question for more details.

Related