I have a set of .txt
files in a folder. For example:
1.txt
2.txt
3.txt
These contain a date following by a filename. For example, 1.txt
may contain:
06/11/2017 randomdocument.txt
06/12/2017 otherdocument.txt
07/11/2017 yadocument.txt
01/02/2017 randomdocument.txt
I want to:
- get the line that matches a particular date regex pattern
- write the line, and the path of the file it is in, to a new document.
My code does the first part. I've tried various iterations with no cigar with the second part. Any help would be appreciated.
Code
Set-Location c:\users\john.smith\desktop\FORREPORT
$files = Get-ChildItem -recurse
$SearchPattern = "0[5,6]{1}/[0-9]\w{1,2}/2017"
Foreach ($file in $files) {
$line = Get-ChildItem -recurse | Get-Content | Select-String $SearchPattern
$d = $line | Select-Object file.FullName
$d | Add-Content -path c:\users\john.smith\desktop\ohsnap.txt
}
Desired Output:
06/12/2017 randomdocument.txt in c:\users\john.smith\desktop\FORREPORT\1.txt