通过forfiles命令自动删除几天前的文件
2012-01-29 11:29
477 查看
自动删除几天前的文件
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
开关很少,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)"
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)"
相关文章推荐
- Linux中通过命令直接删除文件中最后一行
- 通过rman的delete命令删除归档日志文件
- (OK) 通过 git 命令,添加/删除/修改 文件/文件夹
- Linux中通过命令直接删除文件中最后一行
- 通过Android命令自动编译出build.xml文件
- mac通过终端命令删除文件
- 通过Android命令自动编译出build.xml文件
- CMD批处理——forfiles命令使用,自动删除过期备份文件
- file命令查出几天前的日志文件后删除
- 浅谈Linux下通过find命令进行rm文件删除的小技巧
- 如何通过终端命令删除Finder中无法删除的文件
- windows删除几天前文件的批处理命令
- android:通过Android命令自动编译出build.xml文件
- Forfiles命令删除指定天数以前的文件或文件夹
- 删除N天前的M(天)个目录 、删除N天前最后修改的文件 ForFiles, dos command 批处理命令cmd/bat
- 通过ADB命令删除本地文件后,Andorid系统图库不更新的问题
- Linux下通过find命令进行rm文件删除的小技巧
- Linux下通过find命令进行rm文件删除的小技巧
- 自动删除oracle下的过期文件命令
- Windows 下启动前删除Resin日志等文件的bat命令