您当前的位置:首页 > Windows 网站首页Windows
windows下find并清理超过一定时间的文件 forfiles
转载请注明出处:黄桂林的博客 huangguilin.com
在 find命令常用参数 及查找删除N时间之前的文件示例 一文中示例了如何查找旧的无用备份文件进而删除,这个是linux下的命令。
windows也有find命令,windows的命令可以实现同样的功能吗?很遗憾 答案是 否 。
我们查看一下find命令的使用方法,功能非常单一,真是看起来名字是一样的,但是能做的东西却不一样,相差甚远,不禁要吐槽一下微软。在此,find命令相当于linux下的grep
C:\Users\Administrator>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.
但是,windows下有另外一个相似的命令 forfiles
FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {yyyy/MM/dd | dd}]
Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).
/M searchmask Searches files according to a searchmask.
The default searchmask is '*' .
/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".
/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.
To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".
/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"yyyy/MM/dd" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.
/? Displays this help message.
Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C "cmd /c type @file | more"
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C "cmd /c echo @path 0x09 was changed 30 days ago"
FORFILES /D 2001/01/01
/C "cmd /c echo @fname is new since Jan 1st 2001"
FORFILES /D +2017/6/28 /C "cmd /c echo @fname is new today"
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
根据forfiles命令的使用说明,我们需要删除D:\backups目录下超过7天的.log文件,那么命令如下:
forfiles /P D:\backups /M "*.log" /S /C "cmd /c del @path" /D -7
转载请注明出处:黄桂林的博客
转载请注明出处:黄桂林的博客