chris
chris

Reputation: 51

Using get-children to list files with lastwritetime

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?

Upvotes: 5

Views: 58766

Answers (4)

TheUnseen
TheUnseen

Reputation: 354

List all files in all subdirectories and sort them by LastWriteTime (newest write at the end):

Get-ChildItem -Recurse | Sort-Object -Property LastWriteTime | Select-Object LastWriteTime,FullName

Upvotes: 1

Shay Levy
Shay Levy

Reputation: 126732

Try this:

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

Upvotes: 2

Tomas Panik
Tomas Panik

Reputation: 4609

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 }

Upvotes: 2

tenpn
tenpn

Reputation: 4716

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.

Upvotes: 11

Related Questions