2010-11-09 19 views
2

隨着這些代碼行文件:使用Get-孩子列出與lastwritetime

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

我得到d下的一切的列表:\ scripts目錄是在時間戳小於1日齡。輸出:

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 

該協議是,該文件的文件夾(Data_Files & Power_Shell)與在Date參數最後寫。我只是想輸出第3-7行中的文件。

對此提出建議?

回答

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

$_.PSIsContainer適用於文件夾,允許額外的對象過濾它們。

+3

哪裏對象{$ _ lastwritetime -gt(獲取最新).addDays(-1) - 和 - 而不是$。 _.PSIsContainer} ##如果你不需要的話,不要把對象放在哪裏:) – Jaykul 2010-11-09 17:47:05

+0

大的時候謝謝。 – chris 2010-11-09 20:42:44

+0

@jaykul公平點nuff說 – tenpn 2010-11-10 13:37:36

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

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

試試這個:

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

'$ _'不是'$',不是嗎? – tenpn 2010-11-10 13:38:21

+0

呵呵,降價再次襲擊!你必須標記你的代碼,這樣它纔不會被解釋爲標記:) – Jaykul 2010-11-10 20:25:05

+0

是,$ _它是。感謝喬爾,代碼現在已經得到糾正。 – 2010-11-12 09:34:44