0

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
G42
  • 9,791
  • 2
  • 19
  • 34
Athanasius12
  • 37
  • 1
  • 6
  • Are you saying that you want to choose files that have a `LastWriteTime` property in a specific range? – Bill_Stewart Jul 05 '17 at 18:50
  • Your paragraph is hard to follow, but `gci c:\users\john.smith\desktop\FORREPORT -Recurse -pv File| select-string '0[5,6]{1}/[0-9]\w{1,2}/2017' | select Line, @{N='filename';E={$File.FullName}}` ? – TessellatingHeckler Jul 05 '17 at 18:54
  • Hello Bill, Yes they are LastWriteTime, but the reports already exist. So, in other words, I have 3 different .txt files that were run and they contain the LastWriteTime for documents that reside in three separate folders. I am trying to find a way to find all of the line items within each of those those 3 .txt files that meet a specific date criteria (I accomplished this) but then compile them in a new report that gives the path of the line item and which of the three .txt files each line item comes from. Ex. 06/12/2017 Bobburgers.docx located in c:\users\john.smith\desktop\FORREPORTS\1.txt – Athanasius12 Jul 05 '17 at 18:58
  • TessellatingHeckler. Thank you. I know. My paragraph is pretty horrifying; however, I couldn't think of a way to clarify. – Athanasius12 Jul 05 '17 at 18:59
  • @user7431743 I've edited based on what I think you're asking. Please let me know if it's correct. – G42 Jul 05 '17 at 19:14
  • That is absolutely what I am asking. Thanks! – Athanasius12 Jul 05 '17 at 19:18

1 Answers1

0

With minimal changes to the 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) {
  $lines =  Get-Content $file | Select-String $SearchPattern
  if($lines){
      foreach($line in $lines){
        "$line`t$($file.FullName)" | Add-Content -path c:\users\john.smith\desktop\ohsnap.txt
      }
  }
}

This prints each line on a new line in the new document. If you want all matching lines on a single line, remove the foreach:

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) {
  $lines =  Get-Content $file | Select-String $SearchPattern
  if($lines){
        "$lines`t$($file.FullName)" | Add-Content -path c:\users\john.smith\desktop\ohsnap.txt
  }
}

if will also check see that matched lines were actually found so that you don't get ouput of just $file.Fullname with $lines is empty.

PS Your question and code are the best examples I can think of to use this quote. heed it! ;-p

G42
  • 9,791
  • 2
  • 19
  • 34
  • Yes! Thank you gmsOulman. This works! Also, I will heed this. I am self-taught so there is a lot I have missed in form that I probably would have gotten via a formal training, – Athanasius12 Jul 05 '17 at 21:47