当前位置: 代码网 > it编程>前端脚本>Powershell > 用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件

用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件

2024年05月18日 Powershell 我要评论
本来想用批处理的,想想算时间太麻烦了……立马安装powershell看帮助文档,里面有个例子:以下命令查找 program files 文件夹中上次修改日期晚于 2005 年 10 月 1 日并且既不

本来想用批处理的,想想算时间太麻烦了……

立马安装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后创建的所有的文件。。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com