码迷,mamicode.com
首页 > Windows程序 > 详细

windows下定期清理超过一定时间的文件

时间:2016-01-17 00:05:38      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:windows 自动清理

windows下定期清理超过一定时间的文件

背景

linux下当我们想定期清理超过7天无修改的日志或备份文件,可以通过以下命令来完成:

find $path -type f  -mtime|-ctime +7 -exec rm -f {} \;

windows下的find命令,看起来远没有linux下这个强大:

C:\Documents and Settings\qunyingliu>find /?
Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

那要怎么才能完成定时清理超过一定时间的文件,莫非真的要等磁盘告警后人工去处理吗?

找外援,找内助

俗话说,开发靠 Google,运维靠 Baidu,让我们也google一次,发现了一个很有用的windows 命令行,FORFILES。

FORFILES命令的帮助信息:
FORFILES [/P pathname] [/M searchmask] [/S]         [/C command] [/D [+ | -] {yyyy/MM/dd | dd}]

描述:
选择一个文件(或一组文件)并在那个文件上执行一个命令。

参数列表:

    /P    pathname      表示开始搜索的路径。默认文件夹是当前工作的
                        目录 (.)。
    /M    searchmask    根据搜索掩码搜索文件。默认搜索掩码是 ‘*‘。
    /S                  指导 forfiles 递归到子目录。像 "DIR /S"。
    /C    command       表示为每个文件执行的命令。命令字符串应该
                        用双引号括起来。
                        默认命令是 "cmd /c echo @file"。下列变量
                        可以用在命令字符串中:                        @file    - 返回文件名。                        @fname   - 返回不带扩展名的文件名。                        @ext     - 只返回文件的扩展名。                        @path    - 返回文件的完整路径。                        @relpath - 返回文件的相对路径。                        @isdir   - 如果文件类型是目录,返回 "TRUE";
                                   如果是文件,返回 "FALSE"。                        @fsize   - 以字节为单位返回文件大小。                        @fdate   - 返回文件上一次修改的日期。                        @ftime   - 返回文件上一次修改的时间。
                        要在命令行包括特殊字符,字符请以 0xHH
                        形式使用十六进制代码(例如,0x09 为 tab)。
                        内部 CMD.exe 命令前面应以 "cmd /c" 开始。
    /D    date          选择文件,其上一次修改日期大于或等于 (+),
                        或者小于或等于 (-) 用 "yyyy/MM/dd" 格式指定的日期;
                        或选择文件,其上一次修改日期大于或等于 (+)
                        当前日期加 "dd" 天,或者小于或等于 (-) 当前
                        日期减 "dd" 天。有效的 "dd" 天数可以是                        0 - 32768 范围内的任何数字。如果没有指定,                        "+" 被当作默认符号。
    /?                  显示此帮助消息。

问题解决

根据forfiles文件的说明,我们需要删除x:\xxxx目录下超过7天的所有文件,那么命令如下:

forfiles /P x:\xxxx  /S /C "cmd /c del @path" /D -7

当然由于我要删除的x:\xxxx没有子目录,也不需要保留,所以找到7天前的文件都可以删除,如果是需要保留子目录那就要指定/M后面的参数,比如 /M "*.sql" 删除所有找到的超过七天的.sql结尾的文件。

51cto博客一般只会记录一些运维的事情,编辑器不支持markdown。

欢迎来简书来听我吹牛聊马拉松聊生活,http://www.jianshu.com/users/0158c185ef40。

本文出自 “运维者说:从菜鸟到老鸟” 博客,请务必保留此出处http://liuqunying.blog.51cto.com/3984207/1735614

windows下定期清理超过一定时间的文件

标签:windows 自动清理

原文地址:http://liuqunying.blog.51cto.com/3984207/1735614

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!