0

I am attempting to extract text from a series of text files to a .txt document. The text files are stored in a series of sub-directories on the desktop.

So far, I can get the variables from a specific text file using a regular expression, but I need it to search through multiple files spanning across a number of directories - instead of the single file I direct it to.

$input_path = "C:\Users\Darren\Desktop\FolderofData\DataFile1.txt"
$regex = '(\W"_[a-z]+\W)'
$output_file = "C:\Users\Darren\Desktop\Extracted_Data.txt"
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

Is this possible in PowerShell?

1 Answer 1

2

You can pipe FileInfo objects (like those returned from Get-ChildItem) to Select-String:

Get-ChildItem $home\Desktop -Filter Data*.txt |Select-String -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
Sign up to request clarification or add additional context in comments.

1 Comment

For multiple directories get-childitem needs a -recurse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.