打印

[分享] 批量删除N天前的文档

批量删除N天前的文档

公司有一FTP服务器有定时删除某一文件夹下N天前生成的文件的需求,网上搜索了一下。' E9 q1 j8 [1 F! k9 S
找到了一个微软windows2003 自带的小工具forfiles
* q l6 Q( A0 O# w6 x9 d+ k3 F* b6 R- d! m7 h
如果要删除 30天前 生成的 D:\C1 文件夹下面的文件,可以用如下命令:
复制内容到剪贴板
代码:
forfiles /P D:\C1 /D -30 /C "cmd /c del @ file"
[ 本帖最后由 aspirer 于 2007-1-7 20:10 编辑 ]
本帖最近评分记录
  • mwpq 菊花元 +10 Thanks! 2007-1-8 03:45

TOP

谢谢分享,加分鼓励!
: l1 m/ J( B* H
/ S+ v$ B2 n6 v* U k使用vbs应该也可以实现类似功能,但是比较复杂一些。
《无量寿经·第十八愿》言:
设我得佛,十方众生,至心信乐,欲生我国,乃至十念,若不生者,不取正觉。唯除五逆,诽谤正法。

TOP

引用:
原帖由 mwpq 于 2007-1-8 03:49 发表, `5 O& m$ Y: W" H
谢谢分享,加分鼓励!$ q& w/ P7 H( _ [7 P" c
/ Q& i9 r' n1 G9 v# q
使用vbs应该也可以实现类似功能,但是比较复杂一些。
' s$ a6 Y* h) @; N: V9 |
& _2 f0 a7 j/ w. w6 w
偶倒是在网上找到过(CCF),当时测试时,把脚本本身也删除了,怕怕。而且时间的限制好像也不是很对。& c( n$ |) c, D+ y5 r
版版能不能顺便分析一下。
复制内容到剪贴板
代码:
@echo off
>t_day.vbs echo ' Yesterday.vbs, Version 1.01
>>t_day.vbs echo '
>>t_day.vbs echo ' Written by Rob van der Woude
>>t_day.vbs echo ' http://www.robvanderwoude.com
>>t_day.vbs echo '
>>t_day.vbs echo ' Calculate yesterday's date
>>t_day.vbs echo dtmYesterday = DateAdd("d",-%1,Date)
>>t_day.vbs echo '
>>t_day.vbs echo ' Get yesterday's year
>>t_day.vbs echo strYear = DatePart("yyyy",dtmYesterday)
>>t_day.vbs echo '
>>t_day.vbs echo ' Get yesterday's month, add leading zero if necessary
>>t_day.vbs echo If DatePart("m",dtmYesterday) ^< 10 Then
>>t_day.vbs echo strMonth = 0 ^& DatePart("m",dtmYesterday)
>>t_day.vbs echo Else
>>t_day.vbs echo strMonth = DatePart("m",dtmYesterday)
>>t_day.vbs echo End If
>>t_day.vbs echo '
>>t_day.vbs echo ' Get yesterday's day, add leading zero if necessary
>>t_day.vbs echo If DatePart("d",dtmYesterday) ^< 10 Then
>>t_day.vbs echo strDay = 0 ^& DatePart("d",dtmYesterday)
>>t_day.vbs echo Else
>>t_day.vbs echo strDay = DatePart("d",dtmYesterday)
>>t_day.vbs echo End If
>>t_day.vbs echo '
>>t_day.vbs echo ' Format output for yesterday
>>t_day.vbs echo strYest = strYear ^& strMonth ^& strDay
>>t_day.vbs echo '
>>t_day.vbs echo ' echo
>>t_day.vbs echo Wscript.echo( strYest )

@echo on
for /f "delims=" %%i in ('cscript.exe //nologo t_day.vbs') do set t_day=%%i
setlocal ENABLEDELAYEDEXPANSION
for /r %%i in (*.*) do set c_day=%%~ti && if "!c_day:~0,2!!c_day:~3,2!!c_day:~6,2!" lss "%t_day:~2,6%" <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-comffice:smarttags" />del /q "%%i"
endlocal
cls
@echo =========================
@echo 删除完毕.
@echo =========================
[ 本帖最后由 aspirer 于 2007-1-10 15:51 编辑 ]

TOP

楼上,您的脚本其中VBS部分只是用来生成处理,昨天的日期。至于删除文档,则是用批处理命令完成的。" [, C5 Q8 P9 l4 E
下面是一个 批量删除某个日期前的文件的VBS例子,原文地址是:
% h9 K8 @, u, H& f! g$ shttp://www.microsoft.com/technet ... /nov04/hey1104.mspx
) t5 y! | U3 l" R4 S! W( Z; W8 o& O6 o
9 b' R9 O, K: r+ Q+ X: V2 y" `$ K7 l) v/ i. j: x; h
抱歉,没有中文; P+ H- s* ~4 o% ^: q S
) p& p+ Z6 T4 C2 L v% W
Hey, Scripting Guy! I’d like to have a script that can search my computer for all files older than a certain date, and then automatically delete those files. Can I do that?
4 u/ w/ S# s8 G5 l& T" M/ S# C3 N( n
5 M- _% W- L% i. O/ v1 ?-- GM4 Y, R4 R' H5 m' f$ L
& C% C, O; a2 r9 p
- L( d3 b( o% w# x: e! u
Hey, GM. Can you write a script that will search for and delete all the old files on your computer? You bet. Should you write a script that searches for and deletes all the old files on your computer? That’s a separate question that we’ll deal with in a minute.. y1 | I" R- r: B0 d7 V( l3 ?7 c

+ V& N q4 b) q0 OLet’s start with the code itself. Here’s a script that searches for all the files on your computer that were created before November 2, 2003 and then echoes back the name of each file:
8 \1 {9 E5 n ]% V4 }- w5 }1 _: d' T/ f, u3 N, F5 k
strDate = "20031102000000.000000+000"* ^2 C+ c) A0 R( o9 ~

4 x% o# p" H, S \/ n4 I3 WstrComputer = ".") `7 n+ w2 m% C3 U
Set objWMIService = GetObject _
?+ O* U& V$ ?( n& S5 v( B2 @3 t ("winmgmts:\\" & strComputer & "\root\cimv2")
( w# |6 c+ X! Z ?2 F0 b0 ]Set colFiles = objWMIService.ExecQuery _
+ |% _1 [6 k4 h/ k9 D$ i ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'")
O, P7 @! _) j5 b7 FFor Each objFile in colFiles
; x" ^3 F: X+ u: h7 i Wscript.Echo objFile.Name& F9 j! H: @$ M, h5 y+ m; `0 s6 w
Next
' ^# ^- _( z5 E
/ u: o# w8 t- EWe know what you’re thinking: how does the script know that our target date is November 2, 2003? After all, November 2, 2003 doesn’t appear anywhere in the script.
7 R0 H3 R+ P: y9 m {, D* M; ]5 C( ?9 v1 K
Believe it or not, it does; it might not look like a date, but 20031102000000.000000+000 - the value we assign to the variable strDate - represents November 2, 2003. The date looks weird because WMI uses the UTC (Universal Time Coordinate) format. With the UTC format, the first four digits (2003) represent the year; the next two (11) represent the month; and the next two (02) represent the day. In other words, 20031102 is the same as 11/02/2003.
" F* m" B; L3 ~% N5 k# q
3 b4 L9 c3 P) O7 t- JIncidentally, the next six digits represent the hour, minute, and seconds, in 24-hour format; we’ve left these at 0 because we aren’t worried about a specific time (that is, we’re not looking for files created before 2:37 PM on November 2, 2003). The six digits after the period represent milliseconds; those should always be left as zeroes. Finally, the +000 represents the offset from Greenwich Mean Time. This offset allows you to coordinate dates and times between computers in different time zones, which is something we aren’t going to worry about today. Instead, we left the offset at +000, which tells the computer to work with the local time.
- q9 H" x$ w1 z$ W7 G3 A3 r7 P
/ [0 r. X7 K% GSo do we have to pass the date and time in this UTC format? Yes. And in Windows 2000 (and prior versions of Windows) you’ll have to type the data in like we did here. In Windows XP and Windows 2003, however, there’s a new WMI object - SWbemDateTime - that allows you to type in a regular old date (like 11/2/2003), and then automatically converts that date to UTC format.( m. O0 F& T# ~. m

! Y' n% G- d2 I, @3 @Want to know more about SWbemDateTime? Watch the webcast “Thing the Scripting Guys Never Told You,” from Scripting Week 2.4 A% d0 V: d+ l6 ~1 T& b% ]
+ P- q" N& ?) j) N0 q0 \
; y# }( E& g) L ~
After assigning the date to strDate what we have left is just a plain old WMI script, one that searches for all the files with a CreationDate earlier than 11/2/2003. When we find one, we echo the file name.
( g6 ]% |3 n& ~8 F. Z" q% k, c6 O, E/ s1 Q: u# ^
Of course, you asked for a script that deletes old files. And if that’s what you want to do, then just replace the line Wscript.Echo objFile.Name with this line of code:1 l. W+ \5 v' @/ y5 b0 |

( I$ \9 j+ [& Q* I' i: J& jobjFile.Delete' ^4 ]: {! D/ j! N4 D

; } b" K; H9 w' [0 B, I" {4 |Why didn’t we put this line of code in the script for you? Well, we wanted you to think about this script before actually running it. Suppose you have one disk drive (C) on your computer. Do you really want to run a script that deletes all the files created earlier than November 2, 2003? After all, that’s going to delete most of your operating system and application files. Generally speaking, not the sort of thing you want to do.2 t) g' K: {9 Y4 e$ e
# Q5 ?* ?' O" C6 A6 D5 }- f$ N8 C
What that means is that you might need to be a bit more clever when searching for files. For example, maybe your computer has two drives: drive C has your operating system and all your applications, and drive D has all your documents. In that case, you could write a WQL query that searches only drive D for files created before November 2, 2003:; L6 b& R0 J* I% n( `& u8 s

/ y. I" d1 p7 ZstrDate = "20031102000000.000000+000") q x9 s4 A* S+ q/ k- W' i
- r/ r, o$ L/ K2 A1 R& M
strComputer = "."# g2 _( `1 f9 p/ R2 o* |9 \
Set objWMIService = GetObject _* [5 o# @ I0 d4 B. T$ W
("winmgmts:\\" & strComputer & "\root\cimv2")# K3 u, Z+ }- ~- r) ^
Set colFiles = objWMIService.ExecQuery _8 A; P6 B8 W+ Z2 p: T: E" l
("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _1 O3 C1 m. ~. J/ z
" AND Drive = 'D:'")
4 X9 j x5 K8 v: V# C; v2 ~For Each objFile in colFiles& g0 t# }( {4 S
Wscript.Echo objFile.Name
# @/ ]# u( A6 U$ N; n- y bNext
, q- W1 P: `6 r3 u* O# E( g+ _0 o( e
Alternatively, maybe your concern is with old and out-of-date Word documents. In that case, search only for files with a doc file extension that were created prior to November 2, 2003:
9 ^0 g v" ]6 p; D; j8 {( c
: _1 q7 n9 ~7 o9 _5 t% h' J8 nstrDate = "20031102000000.000000+000"
8 k2 x& l# @8 T0 b
# R. N; S J0 ?; V" DstrComputer = "."
8 s6 P' y; b; zSet objWMIService = GetObject _* G( E3 O" u9 I+ p% n: w' B
("winmgmts:\\" & strComputer & "\root\cimv2"). {0 G( Y+ _: x! G; k
Set colFiles = objWMIService.ExecQuery _
* o+ u8 A6 n2 G. Z0 k ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _! G! s, k1 V- V
" AND Extension = 'doc'")
6 }5 w9 i9 I5 z1 o- }, MFor Each objFile in colFiles
. Y+ Z' T9 ^3 K7 A/ [% [ W" U9 j Wscript.Echo objFile.Name: ^$ h+ b$ A. G: [
Next
2 H6 l3 U: y, M, {, S2 ~7 ]' T- P+ \7 P" E* v
There are plenty of other ways to finely-hone your searches; for more details, see the Files and Folders chapter in the Microsoft Windows 2000 Scripting Guide.
% ~! F: g V" C/ i/ v F$ d! m# u2 w) {& N
Incidentally a lot of people ask if it’s possible to search for files based on the last time those files were modified. Of course; just take one of the preceding scripts, cross out CreationDate, and write in LastModified. In other words:8 d M; H9 c) J6 C

& Z! s2 k5 w" W5 s- W5 z1 `" CstrDate = "20031102000000.000000+000"
4 I4 z% ]' e4 S( u1 Q4 I5 d+ z7 `) M4 z! I
strComputer = "."/ f( L$ f! T, {' ^5 I, o
Set objWMIService = GetObject _4 n5 O* k# Q3 d! I; R
("winmgmts:\\" & strComputer & "\root\cimv2")
1 K' z+ a. e( Z2 L9 e F9 p, a1 _3 ]Set colFiles = objWMIService.ExecQuery _) @5 u' U9 M0 C! P4 K; A& P5 S
("Select * From CIM_DataFile Where LastModified < '" & strDate & "'")! k3 r+ }% t2 ]. \) j, a
For Each objFile in colFiles
. [6 \$ J( ^9 B4 E9 f/ F Wscript.Echo objFile.Name: O- S$ ^) j# t Q
Next
《无量寿经·第十八愿》言:
设我得佛,十方众生,至心信乐,欲生我国,乃至十念,若不生者,不取正觉。唯除五逆,诽谤正法。

TOP