Using get-children to list files with lastwritetime

53,801

Solution 1

get-childitem -Path d:\scripts –recurse |  
    where-object {$_.lastwritetime -gt (get-date).addDays(-1)} | 
    where-object {-not $_.PSIsContainer} |
    Foreach-Object { $_.FullName } 

$_.PSIsContainer is true for folders, allowing the extra where-object filters them out.

Solution 2

Try this:

dir d:\scripts –recurse | where {!$_.PSIsContainer -AND $_.lastwritetime -gt (get-date).addDays(-1)} | foreach { $_.FullName }

Solution 3

gci d:\scripts –recurse | 
  ? { $_.Attributes -band [System.IO.FileAttributes]::Archive } |
  ? { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } | 
  foreach { $_.FullName }

or

gci d:\scripts –recurse | 
  ? { -not ($_.Attributes -band [System.IO.FileAttributes]::Directory) } |
  ? { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } | 
  foreach { $_.FullName }
Share:
53,801
Admin
Author by

Admin

Updated on February 29, 2020

Comments

  • Admin
    Admin about 4 years

    With these lines of code:

    get-childitem -Path d:\scripts –recurse | 
    where-object {$_.lastwritetime -gt (get-date).addDays(-1)} |
    Foreach-Object { $_.FullName }
    

    I get a list of everything under the d:\scripts directory that is less than 1 day old in time stamp. Output:

    D:\scripts\Data_Files
    D:\scripts\Power_Shell
    D:\scripts\Data_Files\BackUp_Test.txt
    D:\scripts\Power_Shell\archive_test_1dayInterval.ps1
    D:\scripts\Power_Shell\stop_outlook.ps1
    D:\scripts\Power_Shell\test.ps1
    D:\scripts\WinZip\test.wjf
    

    The deal is, the file folders (Data_Files & Power_Shell) have a last write with in the date param. I just want the files as in lines 3 - 7 in output.

    Suggestions?

  • Jaykul
    Jaykul over 13 years
    where-object {$_.lastwritetime -gt (get-date).addDays(-1) -and -not $_.PSIsContainer} ## Don't double up where-object if you don't need to :)
  • Jaykul
    Jaykul over 13 years
    hehe, markdown strikes again! You have to mark your code so it doesn't get interpreted as markup :)
  • Abdul Hannan Ijaz
    Abdul Hannan Ijaz over 6 years
    used your logic to make a recursive pattern for compression and removal