• 生活就是这样,需要去灌溉!
    • 谢谢您的关注,欢迎您的注册与写作!
    • 循环往复,生生不息,或许这就是生命的意义吧!生命是插曲的产品吧!
    • 古今多少事,都付笑言中!
    • 风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。
    • 闻说双溪春尚好,也拟泛轻舟,只恐双溪舴艋舟,载不动许多愁。

通过bat批处理/forfiles命令/rem命令自动删除几天前的文件

Linux 柳叶扉鸿 来源:csdn-sasa007 7年前 (2018-08-13) 1460次浏览 已收录 扫描二维码
内容目录

Windows 下 bat 批处理,自动获取文件的最后修改时间,然后将指定多少天之前的文件删除,需要 Windows 支持 forfiles 命令,XP 及以上系统都自带 forfiles 命令。
显示当前目录下七天以前的文件路径
forfiles /p %cd% /s /m *.* /d -7 /c “cmd /c echo @path”
自动删除几天前的文件

Windows 2003 2008 测试通过
通过 forfiles 命令找到几天前的数据。

forfiles 的几个参数:
/P 可是搜索的路径。在我们这里就是要在哪个目录寻找要删除的文件
/M 根据搜索掩码搜索文件。默认为*,我们要删除某时间以前的文件。我们只关心时间。
/D 文件修改时间在某个时间之前或者之后。-200 表示 200 天之前的文件。
/C 表示为每个文件执行的命令,这里是要删除该文件所以为”cmd /c del /F /s /q @file”。其中变量@file 表示该文件名。

综上所述,得出下列脚本:

@echo off
echo Del file :::::::::
echo forfiles /P D:\test\DATA /M * /S /D -200 /C "cmd /c del /F /s /q @file"
echo forfiles /P D:\test\DATA /D -200 /C "cmd /c del @file"

echo done
echo . & pase

在 linux 下可以用 find 命令来查找:
find ./ -ctime 1 -name “*mail” -exec rm {} \;
删除一天前修改的文件。

rem 删除前一天的历史数据
forfiles /m *.fc /s /D -1 /c “cmd /c del @file”

rem 删除当前目录下及其子目录中的空文件夹
for /f “tokens=*” %a in (‘dir /b /ad /s E:\FileCache^|sort /r’) do rd “%a” /q 2>nul

在批处理中把%改成%%
for /f “tokens=*” %%a in (‘dir /b /ad /s E:\FileCache^|sort /r’) do rd “%%a” /q 2>nul

Forfiles

windows server 2003 内置命令
开关很少,p 路径,m 方式,s 包含子目录,c 执行命令,d 日期

普通使用可能比不上 for,dir 等, 但是 c 这个开关很强大的

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.

FORFILES /P C:\WINDOWS /S /M DNS*.*
列出 windows 及其子目录下 DNS 开头的所有文件

FORFILES /S /M *.txt /C “cmd /c type @file | more”
列出当前目录以及子目录下所有的 txt 文档的内容,并以分页的形式打印出来

FORFILES /P C:\ /S /M *.bat
列出 windows 及其子目录下的 bat 文件

FORFILES /D -30 /M *.exe /C “cmd /c echo @path 0x09 was changed 30 days ago”

列出 30 天内修 改过的 exe 文件,列出路径+自定义文字 0x09(tab) was changed 30 days ago

FORFILES /D 2001/01/01 /C “cmd /c echo @fname is new since Jan 1st 2001”

列出 2001、0101 后的文件并打印文档名字+is new since Jan 1st 2001

FORFILES /D +2009/4/10 /C “cmd /c echo @fname is new today”

列出 20090410 后修改过的文 件,并打印

FORFILES /M *.exe /D -1

列出一天前到现在修改过的 exe 文件
FORFILES /S /M *.doc /C “cmd /c echo @fsize”

列出 doc 文件,并打印出文件大小

FORFILES /M *.txt /C “cmd /c if @isdir==FALSE notepad.exe @file”

列出 txt 文件, 如果不是文件夹,那么就依次用 notepad 打开该文件,关闭后开启下一个文件。

forfiles /m *.log /c "cmd /c del @file"
forfiles /m *.log /c "cmd /c if @fsize geq 1000000 (del @file)"
forfiles /m *.log /c "cmd /c if @fsize geq 1000000 (del @file) Else (move @file c:\archive)"
forfiles /m *.log /c "cmd /c if @fsize geq 1000000 (echo @file is 1MB or larger) Else (echo @file is 1MB less)"

柳叶扉鸿 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明通过bat批处理/forfiles命令/rem命令自动删除几天前的文件
相关文章 相关文章 相关文章
喜欢 (1)