首页 » ASP代码 » 删除过期文件的vbs脚本

删除过期文件的vbs脚本

技术含量不高,其实windows自带的搜索比这个强。主要是学习fso的用法,如果你能背着写完那以后不用看这方面的了,呵呵。

'本脚本可以指定日期前的所有文件
Const logfiles	= "D:\log_delmssqlfile.txt"
Const startPath	= "D:\wwwroot\ftpname\SiteAdmin\admin"
Const onlyfExt	= ".bak|.tmp|.qq|.csv"
Dim lessTime:lessTime=CDate("2010-7-1 00:00:00")
Dim nsize,nn : nsize=0 : nn=0 : nfile=0
 
Dim fso:Set fso = CreateObject("scripting.filesystemobject")
logs "--- 开始 --- " & Now() & " ---"
If fso.FolderExists(startPath) Then
	Call ScanFolder(startPath)
	Call ScanFolderFile(StartPath)
Else
	WScript.Echo "路径不存在,请检查。"
End If
logs "共扫描" & nfile & "个,删除" & nn & "个,释放了 " & nsize & " KB空间"
logs "--- 完成 --- " & Now() & " ---"
 
Sub ScanFolder(byval path)
	Dim subFolders,nowpath
	Set subFolders=fso.GetFolder(path).SubFolders
	For each subfolder in subFolders 
		nowpath=path & "\" & subfolder.Name
		Call ScanFolder(nowpath)
		Call ScanFolderFile(nowpath)
	Next
End Sub
 
Sub ScanFolderFile(byval folder)
	Dim objfiles
    Set objfiles=fso.GetFolder(folder).Files
    For Each objfile In objfiles
		nfile=nfile+1
		Call delfileTT(objfile.Path,lessTime)
    Next
    Set objfiles=Nothing
End Sub
 
Function delfileTT(byval filename,byref lessTime)
	Dim createTime,isExt,f
	If not fso.FileExists(filename) Or Not IsDate(lessTime) Then
		Exit Function
	End If
	Set f=fso.GetFile(filename)
	createTime=CDate(f.DateCreated)
	strFileExt=LCase(fso.GetExtensionName(filename))
	if onlyfExt="" or instr(onlyfExt&"|","."&strFileExt&"|")>0 then
		isExt=true
	else
		isExt=false
	end if
	If isExt and createTime<lessTime Then
		nn=nn+1
		nsize=nsize + f.Size/1000
		fso.DeleteFile(filename)
		Call logs(nn & Chr(9) & filename)
	End If
	Set f=Nothing
End Function
 
Sub logs(byval str)
	Set f=fso.OpenTextFile(logfiles,8,True)
	f.Write str & vbCrLf
	Set f=Nothing
	WScript.Echo str
End Sub

, , , , , ,

转发到新浪微博 转发到新浪微博

目前这篇文章有10条评论(Rss)

  1. commercial energy | #1
    05/16/2012 at 20:13

    Im obliged for the blog post.Much thanks again. Will read on…

  2. service | #2
    05/16/2012 at 22:02

    Thanks a lot for the article post.Really looking forward to read more. Will read on…

  3. Download Movies | #3
    05/17/2012 at 04:27

    I am so grateful for your article.Really thank you!

  4. Game Graphics | #4
    05/17/2012 at 06:19

    A big thank you for your post.Really thank you! Cool.

  5. Excessive Inquiries | #5
    05/17/2012 at 08:53

    A big thank you for your blog. Cool.

我要评论