2013-05-17 4 views
15

मैं पावरहेल में यह देखने के लिए कैसे देख सकता हूं कि $ fullpath में कोई फ़ाइल "5 दिन 10 घंटे 5 मिनट" से अधिक पुरानी है या नहीं?मैं कैसे जांच सकता हूं कि फ़ाइल PowerShell के साथ किसी निश्चित समय से पुरानी है या नहीं?

(पुराने से, मैं मतलब अगर यह बनाया या संशोधित किया गया था नहीं बाद में 5 दिन 10 घंटे 5 मिनट से)

उत्तर

32

यहाँ यह करने के लिए काफी एक संक्षिप्त अभी तक बहुत पठनीय रास्ता है ऐसा इसलिए है क्योंकि दो तिथियों को घटाकर आपको एक समय मिलता है। टाइम्सपैन मानक ऑपरेटरों के साथ तुलनीय हैं।

उम्मीद है कि इससे मदद मिलती है।

5

इस powershell स्क्रिप्ट 5 दिन, 10 घंटे, और 5 मिनट पुरानी फ़ाइलों को दिखा देंगे। आप एक .ps1 एक्सटेंशन वाली फ़ाइल के रूप में सहेज सकते हैं और फिर इसे चलाएँ:

# You may want to adjust these 
$fullPath = "c:\path\to\your\files" 
$numdays = 5 
$numhours = 10 
$nummins = 5 

function ShowOldFiles($path, $days, $hours, $mins) 
{ 
    $files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)}) 
    if ($files -ne $NULL) 
    { 
     for ($idx = 0; $idx -lt $files.Length; $idx++) 
     { 
      $file = $files[$idx] 
      write-host ("Old: " + $file.Name) -Fore Red 
     } 
    } 
} 

ShowOldFiles $fullPath $numdays $numhours $nummins 

निम्नलिखित लाइन फ़ाइलें फिल्टर के बारे में थोड़ा और अधिक विस्तार है। यह कई पंक्तियों में विभाजित है (कानूनी powershell नहीं हो सकता है) ताकि मैं टिप्पणी नहीं शामिल हो सकते हैं:

$lastWrite = (get-item $fullPath).LastWriteTime 
$timespan = new-timespan -days 5 -hours 10 -minutes 5 

if (((get-date) - $lastWrite) -gt $timespan) { 
    # older 
} else { 
    # newer 
} 

कारण यह काम करता है:

$files = @(
    # gets all children at the path, recursing into sub-folders 
    get-childitem $path -include *.* -recurse | 

    where { 

    # compares the mod date on the file with the current date, 
    # subtracting your criteria (5 days, 10 hours, 5 min) 
    ($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) 

    # only files (not folders) 
    -and ($_.psIsContainer -eq $false) 

    } 
) 
4

Test-Path आपके लिए यह कार्य कर सकते हैं:

Test-Path $fullPath -OlderThan (Get-Date).AddDays(-5).AddHours(-10).AddMinutes(-5) 
+0

-OlderThan स्विच PS2.0 में उपलब्ध नहीं है। यह सुनिश्चित नहीं है कि इसे कब पेश किया गया था, लेकिन यह निश्चित रूप से PS4.0 में उपलब्ध है। – Mike

संबंधित मुद्दे

 संबंधित मुद्दे