定时删除某一个文件夹下面符合要求的文件

gong_max 2010-02-01 04:10:21
一个文件夹下面有很多文件,文件是以当天生成的日期为文件名。
比如文件夹下面有下面几个文件:
2010-01-23.txt
2010-01-24.txt
2010-01-25.txt
2010-01-26.txt
2010-01-27.txt
2010-01-28.txt
2010-01-29.txt
2010-01-30.txt
2010-01-31.txt
2010-02-01.txt

现在的要求是每天做一个定时任务,删除一周之前产生的文件,也就是今天要删除
2010-01-23.txt
2010-01-24.txt
2010-01-25.txt
三个文件。
...全文
1004 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
gong_max 2010-03-04
  • 打赏
  • 举报
回复
十分感谢大家。学习了
prcgolf 2010-02-04
  • 打赏
  • 举报
回复
脚本代码:
'删除 d:\test 目录下的 *.txt 文件(7 天前的)
strComputer = "."
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive='d:' and Path = '\\test\\'")
For Each objFile in colFiles
If DateDiff("d", objFile.LastModified, Date) > 7 Then
objFile.Delete
End If
Next
prcgolf 2010-02-04
  • 打赏
  • 举报
回复
---------------------------
Windows 脚本宿主
---------------------------
脚本: D:\test\x.vbs
行: 6
字符: 5
错误: 类型不匹配: '[string: "20090904131856.00000"]'
代码: 800A000D
源: Microsoft VBScript 运行时错误

---------------------------
确定
---------------------------
p329899301 2010-02-03
  • 打赏
  • 举报
回复
NB啊试试
Microogle 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 xman_78tom 的回复:]
应该是 test 目录下有不符合日期格式的文件名。

其实这是按 lz 要求写的,真正要删除过期文件,应该看文件的创建事件或修改时间。

改一下,
datediff("d", objFile.CreationDate, Date) 或 datediff("d", objFile.LastModified, Date)
[/Quote]
1.用On Error Resume Next
If Err Then
2.在WQL里面用Like,最好先修改Now来做模板。
xman_78tom 2010-02-03
  • 打赏
  • 举报
回复
应该是 test 目录下有不符合日期格式的文件名。

其实这是按 lz 要求写的,真正要删除过期文件,应该看文件的创建事件或修改时间。

改一下,
datediff("d", objFile.CreationDate, Date) 或 datediff("d", objFile.LastModified, Date)
prcgolf 2010-02-03
  • 打赏
  • 举报
回复
---------------------------
Windows 脚本宿主
---------------------------
脚本: D:\test\x.vbs
行: 5
字符: 5
错误: 类型不匹配: '[string: "theworld"]'
代码: 800A000D
源: Microsoft VBScript 运行时错误

---------------------------
确定
---------------------------
readfuture 2010-02-02
  • 打赏
  • 举报
回复
d
lang071234 2010-02-02
  • 打赏
  • 举报
回复
VBS
ft3188398 2010-02-02
  • 打赏
  • 举报
回复
vba??代码收藏,学习了
xman_78tom 2010-02-02
  • 打赏
  • 举报
回复
不会吧。
测试数据
mkdir e:\test
cd test
echo > 2010-01-30.txt
echo > 2010-01-20.txt

prcgolf 2010-02-02
  • 打赏
  • 举报
回复
DateDiff("d", objFile.FileName, Date)
这句话报错
xman_78tom 2010-02-01
  • 打赏
  • 举报
回复

strComputer = "."
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive='e:' and Path = '\\test\\'")
For Each objFile in colFiles
If DateDiff("d", objFile.FileName, Date) > 7 Then
objFile. Delete
End If
Next
xman_78tom 2010-02-01
  • 打赏
  • 举报
回复

'删除 e:\test 目录下的 *.txt 文件(7 天前的)
strComputer = "."
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive='e:' and Path = '\\test\\'")
For Each objFile in colFiles
If DateDiff("d", objFile.FileName, Date) <= 7 Then
objFile. Delete
End If
Next
  • 打赏
  • 举报
回复
vbs 对日期的处理好一点。
laoyebin 2010-02-01
  • 打赏
  • 举报
回复
用EXCEL+VBA就简单多了
就是just4 2010-02-01
  • 打赏
  • 举报
回复
就是每天删除7天以前的文件,这里的麻烦事就是跨年月时不好弄,可试下:
[code=BatchFile]@echo off
echo 今天日期是:%date%
set yy=%date:~0,4%
set mm=%date:~5,2%
set dd=%date:~8,2%
set /a dt=%yy%*365+%mm%*31+%dd%
echo.
echo 开始删除7天前的文件,加上20*前缀避免错误
echo.
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in ('dir c:\test\20*.txt /b') do (
set ff=%%~ni
set yf=!ff:~0,4!
set mf=!ff:~5,2!
set df=!ff:~8,2!
set /a ft=!yf!*365+!mf!*31+!df!
set /a res=%dt%-!ft!
if !res! geq 7 (echo 删除文件 %%i 日期间隔 !res!)&&(del %%i /f /q)
)
echo.
echo 删除完成
pause>nul[/code]
dl0903_zyf0705 2010-02-01
  • 打赏
  • 举报
回复
受教了 多谢
可乐是我 2010-02-01
  • 打赏
  • 举报
回复
忘了给上地址了 补上:http://blog.csdn.net/ljc007/archive/2010/01/16/5198756.aspx
可乐是我 2010-02-01
  • 打赏
  • 举报
回复
这个就用007的那个删除指定天数之前的批处理 就可以了

6,850

社区成员

发帖
与我相关
我的任务
社区描述
Windows 2016/2012/2008/2003/2000/NT
社区管理员
  • Windows Server社区
  • qishine
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧