मैं केवल उन फ़ाइलों को हटाना चाहता हूं जो किसी विशेष फ़ोल्डर में 15 दिन पहले बनाई गई थीं। मैं PowerShell का उपयोग करके यह कैसे कर सकता हूं?
मैं केवल उन फ़ाइलों को हटाना चाहता हूं जो किसी विशेष फ़ोल्डर में 15 दिन पहले बनाई गई थीं। मैं PowerShell का उपयोग करके यह कैसे कर सकता हूं?
जवाबों:
दिए गए उत्तर केवल फाइलों को हटा देंगे (जो कि माना जाता है कि इस पोस्ट के शीर्षक में क्या है), लेकिन यहां कुछ कोड हैं जो पहले 15 दिनों से अधिक पुरानी सभी फ़ाइलों को हटा देंगे, और फिर किसी भी खाली निर्देशिका को पुन: हटा सकते हैं जो शायद छोड़ दिया गया हो पीछे। मेरा कोड -Forceछिपी और पढ़ने वाली फ़ाइलों को भी हटाने के लिए विकल्प का उपयोग करता है । इसके अलावा, मैं उपनाम का उपयोग नहीं करने के लिए के रूप में ओ पी PowerShell के लिए नई है चुना है और नहीं समझ सकते हैं क्या gci, ?, %, आदि कर रहे हैं।
$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
और निश्चित रूप से अगर आप यह देखना चाहते हैं कि वास्तव में उन्हें हटाने से पहले कौन सी फाइलें / फ़ोल्डर हटाए जाएंगे, तो आप दोनों लाइनों के अंत में -WhatIfस्विच को Remove-Itemcmdlet कॉल में जोड़ सकते हैं।
यहां दिखाया गया कोड PowerShell v2.0 संगत है, लेकिन मैं अपने ब्लॉग पर इस पुन: प्रयोज्य कार्यों के रूप में इस कोड और तेज़ PowerShell v3.0 कोड को भी दिखाता हूं ।
बस (PowerShell V5)
Get-ChildItem "C:\temp" -Recurse -File | Where CreationTime -lt (Get-Date).AddDays(-15) | Remove-Item -Force
दूसरा तरीका वर्तमान तिथि से 15 दिन घटाकर CreationTimeउस मूल्य से तुलना करना है:
$root = 'C:\root\folder'
$limit = (Get-Date).AddDays(-15)
Get-ChildItem $root -Recurse | ? {
-not $_.PSIsContainer -and $_.CreationTime -lt $limit
} | Remove-Item
मूल रूप से, आप दिए गए पथ के तहत फ़ाइलों पर पुनरावृति करते हैं, CreationTimeवर्तमान समय से मिली प्रत्येक फ़ाइल को घटाते हैं , और Daysपरिणाम की संपत्ति के खिलाफ तुलना करते हैं। -WhatIfस्विच क्या वास्तव में फ़ाइलों को हटाने (जो फ़ाइलें हटा दी जाएंगी) के बिना क्या होगा आपको बता देंगे, स्विच को दूर वास्तव में फ़ाइलों को हटाने के लिए:
$old = 15
$now = Get-Date
Get-ChildItem $path -Recurse |
Where-Object {-not $_.PSIsContainer -and $now.Subtract($_.CreationTime).Days -gt $old } |
Remove-Item -WhatIf
इसे इस्तेमाल करे:
dir C:\PURGE -recurse |
where { ((get-date)-$_.creationTime).days -gt 15 } |
remove-item -force
-recurseएक बहुत अधिक है, नहीं? डीआईआर सूची पुनरावर्ती है, आइटम का विलोपन शामिल चिल्ड के साथ नहीं होना चाहिए, है ना?
Esperento57 की स्क्रिप्ट पुराने PowerShell संस्करणों में काम नहीं करती है। यह उदाहरण है:
Get-ChildItem -Path "C:\temp" -Recurse -force -ErrorAction SilentlyContinue | where {($_.LastwriteTime -lt (Get-Date).AddDays(-15) ) -and (! $_.PSIsContainer)} | select name| Remove-Item -Verbose -Force -Recurse -ErrorAction SilentlyContinue
यदि आपको विंडोज 10 बॉक्स पर उपरोक्त उदाहरणों के साथ समस्या हो रही है, तो उसके .CreationTimeसाथ बदलने का प्रयास करें .LastwriteTime। इसने मेरे लिए काम किया।
dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force
LastwriteTimeजैसा है वैसा ही नहीं है CreationTime, LastwriteTimeहर बार फ़ाइल को संशोधित करने के बाद अपडेट किया जाता है।
#----- Define parameters -----#
#----- Get current date ----#
$Now = Get-Date
$Days = "15" #----- define amount of days ----#
$Targetfolder = "C:\Logs" #----- define folder where files are located ----#
$Extension = "*.log" #----- define extension ----#
$Lastwrite = $Now.AddDays(-$Days)
#----- Get files based on lastwrite filter and specified folder ---#
$Files = Get-Children $Targetfolder -include $Extension -Recurse | where {$_.LastwriteTime -le "$Lastwrite"}
foreach ($File in $Files)
{
if ($File -ne $Null)
{
write-host "Deleting File $File" backgroundcolor "DarkRed"
Remove-item $File.Fullname | out-null
}
else
write-host "No more files to delete" -forgroundcolor "Green"
}
}
$Filesखाली है तो यह फॉर्च्यूमेंट स्टेटमेंट में प्रवेश नहीं करेगा । आप में foreach रखना चाहिए अगर बयान।