源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-04-28 00:35 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件
本来想用批处理的,想想算时间太麻烦了…… 立马安装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)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部