用PowerShell刪除N天前或指定日期(前后)創(chuàng)建(或修改)的文件
本來(lái)想用批處理的,想想算時(shí)間太麻煩了……
立馬安裝PowerShell看幫助文檔,里面有個(gè)例子:
以下命令查找 Program Files 文件夾中上次修改日期晚于 2005 年 10 月 1 日并且既不
小于 1 MB 也不大于 10 MB 的所有可執(zhí)行文件(測(cè)試發(fā)現(xiàn)沒(méi)法運(yùn)行-_-!):
Get-ChildItem -Path $env:ProgramFiles -Recurse -Include *.exe | Where-Object `
-FilterScript {($_.LastWriteTime -gt "2005-10-01") -and ($_.Length -ge 1m) `
-and ($_.Length -le 10m)}
改了一下成為下面的,以刪除D:\test及子目錄里10天前創(chuàng)建的文件為例,測(cè)試請(qǐng)謹(jǐn)慎!
因?yàn)閮?nèi)容太長(zhǎng)顯示成多行,實(shí)際上是一行。用“`”字符作為延續(xù)符(雙引號(hào)內(nèi)的,是重
音符不是單引號(hào)),相當(dāng)于vbs的“_”,它告訴Windows PowerShell下一行是延續(xù)部分,
它在整行如果不換行就無(wú)法置于庫(kù)中這種情況下有用。只允許將表達(dá)式作為管道的第一
個(gè)元素。
一行命令取得過(guò)期文件列表:
Get-ChildItem -Path D:\test -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 10 `
-and $_.PsISContainer -ne $True)} | Select-Object FullName
一行命令刪除過(guò)期文件:
Get-ChildItem -Path D:\test -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 10 `
-and $_.PsISContainer -ne $True)} | Remove-Item
一行命令刪除過(guò)期文件(包括刪除只讀、隱藏):
Get-ChildItem -Path D:\test -Force -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 10 `
-and $_.PsISContainer -ne $True)} | Remove-Item -Force
當(dāng)然,可以用別名簡(jiǎn)寫(xiě)命令。
或者先在Types.ps1xml文件里找到System.IO.FileInfo,增加Age成員:
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>Age</Name>
<GetScriptBlock>
((get-date) - ($this.creationtime)).days
</GetScriptBlock>
</ScriptProperty>
</Members>
添加的內(nèi)容是從<ScriptProperty>到</ScriptProperty>,修改后以后不用再加。
腳本內(nèi)容:
ForEach ($file in Get-ChildItem D:\test\* -Force -Recurse `
-ErrorAction:SilentlyContinue)
{
if (($file).Age -ge 10 -and $file.PsISContainer -ne $True)
{$file.Delete()}
}
這里不能使用{Remove-Item -Force "$file"}
腳本擴(kuò)展名是.ps1,擴(kuò)展名里的是數(shù)字1。
-gt是大于,-ge是大于或等于,其他看幫助。
如果PSIsContainer屬性為真那意味著處理的是文件夾而不是文件。
-Force是包括只讀、隱藏等系統(tǒng)文件,用了它之后最好用-ErrorAction。
-ErrorAction:SilentlyContinue作用是不顯示錯(cuò)誤繼續(xù)執(zhí)行腳本,比如遞歸時(shí)遇到
System Volume Information等無(wú)權(quán)限進(jìn)入的目錄就會(huì)出錯(cuò)。
查找指定日期前創(chuàng)建的文件:
Get-ChildItem -Path D:\test -Force -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {($_.CreationTime -gt "2011-01-01") -and `
($_.PsISContainer -ne $True)} | Select-Object FullName
查找指定日期前修改的文件:
Get-ChildItem -Path D:\test -Force -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {($_.LastWriteTime -gt "2011-01-01") -and `
($_.PsISContainer -ne $True)} | Select-Object FullName
如果要?jiǎng)h除,Select-Object FullName改成Remove-Item -Force
指定日期的用批處理還是很方便,如果要指定刪除N天前的舊文件就麻煩了點(diǎn),
下面的示例是用bat刪除指定日期修改過(guò)的文件。注意是修改,不是創(chuàng)建,只
有dir /tc才能查看到文件創(chuàng)建時(shí)間,默認(rèn)dir都是dir /tw
發(fā)信人: nwn (Lie), 信區(qū): DOS
標(biāo) 題: Re: (for命令)批量刪除某一時(shí)間段內(nèi)創(chuàng)建的文件?
發(fā)信站: 水木社區(qū) (Sat Jun 7 08:39:39 2008), 站內(nèi)
@echo off cd /d 你的目錄 for %%f in (*) do if "%%~tf" gtr "2008-04-01" del "%%f"
如果要包含子目錄,使用 for /r . %%f in ....
【 在 justzhou (玉樹(shù)臨風(fēng)) 的大作中提到: 】
: 比如要?jiǎng)h除某目錄下2008-04-01后創(chuàng)建的所有的文件。。
相關(guān)文章
powershell 將文本轉(zhuǎn)換成表格的另一種可行方式
這篇文章主要介紹了powershell 將文本轉(zhuǎn)換成表格的另一種可行方式,需要的朋友可以參考下2014-08-08
Powershell實(shí)現(xiàn)從注冊(cè)表獲取用戶配置腳本分享
這篇文章主要介紹了Powershell實(shí)現(xiàn)從注冊(cè)表獲取用戶配置腳本分享,本文直接給出實(shí)現(xiàn)腳本源碼,需要的朋友可以參考下2015-03-03
PowerShell Out-File向只讀文件寫(xiě)入內(nèi)容的方法
這篇文章主要介紹了PowerShell Out-File向只讀文件寫(xiě)入內(nèi)容的方法,只需要加一個(gè)-Force參數(shù)即可,需要的朋友可以參考下2014-08-08
PowerShell函數(shù)使用正則表達(dá)式驗(yàn)證輸入?yún)?shù)實(shí)例
這篇文章主要介紹了PowerShell函數(shù)使用正則表達(dá)式驗(yàn)證輸入?yún)?shù)實(shí)例,即檢驗(yàn)輸入?yún)?shù)是否符合正則規(guī)則,需要的朋友可以參考下2014-07-07
PowerShell Contains函數(shù)查找字符串實(shí)例
這篇文章主要介紹了PowerShell Contains函數(shù)查找字符串實(shí)例,Contains函數(shù)的作用是查詢一個(gè)字符串中是否存在另一個(gè)字符串,需要的朋友可以參考下2014-08-08
PowerShell中簡(jiǎn)單的自定義函數(shù)和調(diào)用函數(shù)例子
這篇文章主要介紹了PowerShell中簡(jiǎn)單的自定義函數(shù)和調(diào)用函數(shù)例子,非常簡(jiǎn)單的一個(gè)小例子,需要的朋友可以參考下2014-08-08

