Daria Da
Daria Da

Reputation: 19

select-string stuck in endless loop

I am trying to extract all text matching a particular pattern from files in the directory using PowerShell. However, for some reason it seems to loop indefinitely (output is an endlessly repeating set of the same results and the script never finishes). Please, can anyone give me a tip on what the problem is?

$input_path = 'C:\Users\*.txt'
$output_file = 'C:\Users\*.txt'
$regex = '[A-Za-z]+?_V[A-Z][A-Z0]?[A-Z]? [A-Za-z]+?_R[A-Z][A-Z]?V?'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

Upvotes: 1

Views: 299

Answers (1)

Dave Sexton
Dave Sexton

Reputation: 11188

As pointed out in the comments you should probably save the output to a different folder or capture the files (not just the path) into a variable. Something like this:

$input_files = Get-Childitem 'C:\Users\*.txt'
$output_file = 'C:\Users\output.txt'
$regex = '[A-Za-z]+?_V[A-Z][A-Z0]?[A-Z]? [A-Za-z]+?_R[A-Z][A-Z]?V?'
($input_files | Select-String -Pattern $regex -AllMatches).Matches.Value > $output_file

Upvotes: 1

Related Questions