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 li开发者_StackOverflow中文版st 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?
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.
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 }
Try this:
dir d:\scripts –recurse | where {!$_.PSIsContainer -AND $_.lastwritetime -gt (get-date).addDays(-1)} | foreach { $_.FullName }
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
精彩评论