powershellgetdateselect-string

Look for files with today's date and look for pattern in file, print what found


I'm trying to find the powershell equivalent of this Windows shell command, except now I'm looking for files with today's date:

findstr /S /C:"Firmware version text" C:\Temp\Filename1* & findstr /S /C:"Class::Method1" C:\Temp\Filename1* & echo "FindingFailures in today files"

I'm testing this, and it's printing here and the dates, and filename, but it's not printing the text in the file. I know that what I'm looking for is in that file, since I found it running the windows shell command without the date part/powershell.

This is the powershell:

 Set-Location -Path "C:\Temp\"
 Get-Item Filename1* | 
 Foreach { 
     Write-Host "here"
     $lastupdatetime=$_.LastWriteTime
     $nowtime = get-date
     Write-Host $nowtime.Date
     Write-Host (Get-Date).Date
     if ($nowtime.Date -eq (Get-Date).Date) 
     {
           Write-Host $_.Name
           if(Select-String -Path $_.Name -Pattern "'Firmware version text'" ) {write-host "Found Firmware version"}
           if(Select-String -Path $_.Name -Pattern "'Class::Method1'" )  {write-host "Found Class::Method1"}
     }
 }

Running it, I see:

here
3/28/2023 12:00:00 am
3/28/2023 12:00:00 am
Filename1.txt

But it's not showing the lines in the file that I know are there. Any ideas how to get it to fix the issue with finding the text in the file(s) and print that it's found? Possibly print the line with that text from the file?

Update

Thank you Santiago, I'm trying to get the date portion to work, but it's finding older files and I just want today's dated files.

write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
 Get-Item "C:\Temp\Filename1*" | Foreach {$_.Name;$_.LastWriteTime
 Where-Object  { $_.LastWriteTime.date -ge (get-date).Date } |
           Write-host $_.Name
           write-host $_.LastWriteTime
           Write-host "-----mid---"
           #Select-String  -path $_.Name -Pattern "'Firmware version text'"
           #Select-Object Filename, LineNumber, Line, Path  
           } 
  
  Write-host "-----end---"

For this the printouts are:

--start--
get date: 3/282023 12:00:00 AM
--find files in date next--
Filename1.txt

Tuesday, March 28, 2023 3:57:15 PM
3/28/2023 3:57:15 PM
----mid--
Filename11.txt
Thursday, March 23, 2023 9:23:59 AM
3/23/2023 9:23:59 AM
---mid--
---end--

So it's finding an older file, but I just want files with today's date. Time today isn't important.

Update1:

I had tried exactly what Santiago had, but it wasn't working

Get-Item "C:\Temp\Filename1*" | 
Where-Object { $_.LastWriteTime.date -ge [datetime]::Today } | 
  Write-host $_.Name

error:

Write-host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters 
that take pipeline input.

Update2

There are multiple files in the dir by that similar name, so maybe that's why Santiago's works but mine doesn't. Also, the time comparison isn't working for me that Santiago shows in his solution. I'm not sure how to get that to work and I'm not finding helpful results with an internet search. This is my current try:

$nowtime = (get-date).Date
 write-host $nowtime
 write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
 Get-Item "C:\Temp\Filename1*" | 
 Foreach { $_.Name; write-host $_.LastWriteTime
 Where-Object  { $nowtime -eq $_.LastWriteTime.date  } |
           write-host "here"
           Write-host $_.Name
 }

which prints:

3/29/2023 12:00:00 AM ---start--- get date: 3/29/2023 12:00:00 AM --find files in date next-- filename11.txt 3/29/2023 8:58:09 AM filename11.txt filename13.txt 3/23/2023 9:23:59 AM filename13.txt

Update3 I'm trying exactly what Santiago is showing, and it's showing the error:

[datetime]::Today
 write-host "---start---"
 write-host "get date:"(get-date).date
 write-host "--find files in date next--"
Get-Item "C:\Temp\Filename1*" | 
 Where-Object  { $_.LastWriteTime.date -ge [datetime]::Today } |
          Select-String  -path $_.Name -Pattern "'Firmware version text'" |
           Select-Object Filename, LineNumber, Line, Path  

Error:

Select-String : Cannot bind argument to parameter 'Path' because it is null.

However the following is working for me:

Update4:

$nowtime = (get-date).Date
Get-Item "C:\Temp\Filename1*" | 
 Foreach { $lastupdatetime=$_.LastWriteTime  $_.LastWriteTime
 if  ( $nowtime -eq $lastupdatetime.date  ) {
           Select-String  -path $_.FullName -Pattern "Firmware version "  | 
              Select-Object Filename, LineNumber, Line, Path;
              if(Select-String -path $_.FullName -Pattern "Firmware version text2"  )
                {Select-String -path $_.FullName -Pattern "Class::Method1"}
}
}

Prints:

Filename                                                LineNumber Line                                                                                                                        Path        
--------                                                ---------- ----                                                                                                                        ----        
filename1.txt         10 01-17-2023 12:22:27 PM     Firmware version : unknown                                                                          C:\Temp\...
filename1.txt         46 01-17-2023 12:22:27 PM     Firmware version : FW="K.02"                                                         C:\Temp\...
filename1.txt.txt          8 01-17-2023 12:22:17 PM     Error in text1():  Class::Method1() failed.                                     C:\Temp\...
filename1.txt.txt         11 Class::Method1() failed.                                                                                     C:\Temp\...
filename1.txt.txt         97 01-17-2023 12:22:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        176 01-17-2023 12:22:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        177 01-17-2023 12:23:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        180 01-17-2023 12:23:25 PM     Error in text2::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        181 01-17-2023 12:23:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        182 01-17-2023 12:23:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        183 01-17-2023 12:24:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        184 01-17-2023 12:24:25 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        185 01-17-2023 12:24:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        186 01-17-2023 12:24:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        203 01-17-2023 12:25:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
filename1.txt.txt        206 01-17-2023 12:25:21 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...

Note, there's multiple files in the dir with the correct name signature, but right now only one has today's date.


Solution

  • This is working:

    Update4:

    $nowtime = (get-date).Date
    Get-Item "C:\Temp\Filename1*" | 
     Foreach { $lastupdatetime=$_.LastWriteTime  $_.LastWriteTime
     if  ( $nowtime -eq $lastupdatetime.date  ) {
               Select-String  -path $_.FullName -Pattern "Firmware version "  | 
                  Select-Object Filename, LineNumber, Line, Path;
                  if(Select-String -path $_.FullName -Pattern "Firmware version text2"  )
                    {Select-String -path $_.FullName -Pattern "Class::Method1"}
    }
    }
    

    Prints:

    Filename                                                LineNumber Line                                                                                                                        Path        
    --------                                                ---------- ----                                                                                                                        ----        
    filename1.txt         10 01-17-2023 12:22:27 PM     Firmware version : unknown                                                                          C:\Temp\...
    filename1.txt         46 01-17-2023 12:22:27 PM     Firmware version : FW="K.02"                                                         C:\Temp\...
    filename1.txt.txt          8 01-17-2023 12:22:17 PM     Error in text1():  Class::Method1() failed.                                     C:\Temp\...
    filename1.txt.txt         11 Class::Method1() failed.                                                                                     C:\Temp\...
    filename1.txt.txt         97 01-17-2023 12:22:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        176 01-17-2023 12:22:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        177 01-17-2023 12:23:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        180 01-17-2023 12:23:25 PM     Error in text2::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        181 01-17-2023 12:23:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        182 01-17-2023 12:23:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        183 01-17-2023 12:24:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        184 01-17-2023 12:24:25 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        185 01-17-2023 12:24:40 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        186 01-17-2023 12:24:55 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        203 01-17-2023 12:25:10 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...
    filename1.txt.txt        206 01-17-2023 12:25:21 PM     Error in text1::text2:  Class::Method1() failed. C:\Temp\...