本来想用批处理的,想想算时间太麻烦了……
立马安装PowerShell看帮助文档,里面有个例子:
以下命令查找 Program Files 文件夹中上次修改日期晚于 2005 年 10 月 1 日并且既不
小于 1 MB 也不大于 10 MB 的所有可执行文件(测试发现没法运行-_-!):
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天前创建的文件为例,测试请谨慎!
因为内容太长显示成多行,实际上是一行。用“`”字符作为延续符(双引号内的,是重
音符不是单引号),相当于vbs的“_”,它告诉Windows PowerShell下一行是延续部分,
它在整行如果不换行就无法置于库中这种情况下有用。只允许将表达式作为管道的第一
个元素。
一行命令取得过期文件列表:
Get-ChildItem -Path D:\test -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 10 `
-and $_.PsISContainer -ne $True)} | Select-Object FullName
一行命令删除过期文件:
Get-ChildItem -Path D:\test -Recurse -ErrorAction:SilentlyContinue | `
Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 10 `
-and $_.PsISContainer -ne $True)} | Remove-Item
一行命令删除过期文件(包括删除只读、隐藏):
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
当然,可以用别名简写命令。
或者先在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>
添加的内容是从<ScriptProperty>到</ScriptProperty>,修改后以后不用再加。
脚本内容:
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"}
脚本扩展名是.ps1,扩展名里的是数字1。
-gt是大于,-ge是大于或等于,其他看帮助。
如果PSIsContainer属性为真那意味着处理的是文件夹而不是文件。
-Force是包括只读、隐藏等系统文件,用了它之后最好用-ErrorAction。
-ErrorAction:SilentlyContinue作用是不显示错误继续执行脚本,比如递归时遇到
System Volume Information等无权限进入的目录就会出错。
查找指定日期前创建的文件:
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
如果要删除,Select-Object FullName改成Remove-Item -Force
指定日期的用批处理还是很方便,如果要指定删除N天前的旧文件就麻烦了点,
下面的示例是用bat删除指定日期修改过的文件。注意是修改,不是创建,只
有dir /tc才能查看到文件创建时间,默认dir都是dir /tw
发信人: nwn (Lie), 信区: DOS
标 题: Re: (for命令)批量删除某一时间段内创建的文件?
发信站: 水木社区 (Sat Jun 7 08:39:39 2008), 站内
@echo off cd /d 你的目录 for %%f in (*) do if "%%~tf" gtr "2008-04-01" del "%%f"
如果要包含子目录,使用 for /r . %%f in ....
【 在 justzhou (玉树临风) 的大作中提到: 】
: 比如要删除某目录下2008-04-01后创建的所有的文件。。
更新日志
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】