`
bluethink
  • 浏览: 98356 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[原创]自制的Windows下的文件清理工具

阅读更多
最近在一项目里需定期清理指定路径下的过期文件,因为客户用的是windows操作系统,因此首先想到用windows脚本来实现,
这里的Windows脚本是指Windows Script Host(WSH Windows脚本宿主),因为目前的的清理只是把文件移动到不同的地方,因此这里的清理还不算真正的清理,根据要求可以修改

脚本实现下面的下功能:
1.批处理方式执行清理
  在配置文件中添加相应的配置可以对多个路径下的过期文件执行清理


2.单步操作方式执行清理
  指定单独的路径,执行清理工具只清理此路径下的过期文件
  例如执行 movePlan 10 c:\tempfile d:\tempfile
会把c:\tempfile从当前日期算起超过10天的文件移动到d:\tempfile里面



脚本由以下3个文件组成
代码文件:moveFiels.vbs
批处理文件:movePlan.bat
配置文件:pathList.txt

配置文件的参数格式:
<过期的天数1>,<源路径1>,<目标路径1>
<过期的天数2>,<源路径2>,<目标路径2>
<过期的天数3>,<源路径3>,<目标路径3>


配置文件的参数要求:
  • 各字段用半角逗号分隔
  • 路径配置文件文件名不可以改变或移动位置,需要和上面的两个文件在同一路径下
  • 文件里可以配置多个记录,用于监控多个路径下的文件
  • 源路径和目标路径必须是有效的,源路径和目标路径不可以相同,并且要用绝对路径
  • 保留文件的天数必须是整数且最小大于5,如果保留的文件的天数小于5,按5天处理
  • 目标路径下的同名文件会被源路径下的文件强制覆盖
  • 配置文件里每行为一个配置记录,如果某一行的配置参数小于3个,此配置记录将被忽略
  • 如果原路径或目标不是有效的路径,此配置记录将被忽略


on error resume Next

Set objArg = Wscript.Arguments
If (objArg.Count=3) Then 
  Set fso=CreateObject("Scripting.FileSystemObject")
  checkPlan objArg(0), objArg(1), objArg(2),fso
Else 
  MonitorProcess()
End If 


Function MonitorProcess()
  Set fso=CreateObject("Scripting.FileSystemObject")
	Dim filename,strFileLine,objMonitorFile,fso,arrProcessParaInfo
	filename = "pathlist.txt"
	If Not (fso.fileexists(filename)) Then
        WriteLog("没有找到文件 "& filename & "!" &vbCrLf&"请确定文件的位置!")
        Wscript.Quit
	End If

	Set objMonitorFile = fso.OpenTextFile(filename,1)
	Do While objMonitorFile.AtEndOfStream <> True
		WriteLog (" ")
        strFileLine = trim(objMonitorFile.ReadLine)		
		arrProcessParaInfo = split(strFileLine,",")
		
		'chaeck parameters count
		arrLength =  UBound(arrProcessParaInfo)

		If arrLength=2 Then

			checkPlan trim(arrProcessParaInfo(0)),trim(arrProcessParaInfo(1)),trim(arrProcessParaInfo(2)),fso

		Else 

			WriteLog("配置错误: 配置参数应该是3个, 原始配置:" & strFileLine)

		End If
    Loop

	objMonitorFile.Close

	Set objMonitorFile = Nothing
	Set objFso         = Nothing

End Function

Sub checkPlan(recDays,srcPath,destPath,fso)	
	'string convert to int
	recDays = Cint(trim(recDays))
		
	srcpath= trim(srcPath) 
	destpath = trim(destPath)
	
	'check days
	If (recDays<5) Then 
			recDays = 5
	End If 
			
	'check path
	if (fso.FolderExists(srcpath)) And (fso.FolderExists(destpath))  Then 			
		process recDays,srcpath,destpath,fso
		'WriteLog("OK 准备移动文件")
	Else 
		WriteLog("源路径或目标路径不存在")
	End If	 
End Sub 


Sub Process(recDays,srcPath,destPath,fso)
	count = 0
	deleCount = 0
	overWrite =""

	set srcPathTemp=fso.getfolder(srcPath)
	set files=srcPathTemp.files
	fileCount = fso.GetFolder(srcPath).Files.Count
	WriteLog ("开始执行清理.....")
	WriteLog ("原始路径 " &  srcPath  & "  共 " & fileCount  & " 个文件" )
	WriteLog ("目标路径 " &  destPath)
	If (fileCount < 1 ) Then 
		WriteLog ("原始路径下没有任何文件 ")
		Exit sub
	End If 	

	for each file in files
		getfso=fso.GetFile(file).DateLastModified
		diff = DateDiff("D", CDate(getfso), CDate(Date))
		If diff > recDays Then
			
			destfile = destPath & "\" & file.name
			'if file is opend enforce deleted
			If fso.fileExists(destfile) Then 				
				deleCount = deleCount + 1
				fso.DeleteFile destfile
			End If
			
			fso.MoveFile  file , destPath & "\" & file.name
			count = count + 1			
		End if
	 Next

	 If deleCount >0 Then 
		overWrite = "覆盖目标路径下 " & deleCount & " 个同名个文件."
	 End If 


	 If count >0 Then 		
		WriteLog ("已经移动 " & count & " 个过期文件到目标路径下. " & overWrite )
	 Else 
		WriteLog ("该路径下没有过期个文件.")
	 End If 
end Sub

Function WriteLog(strLog)
    Wscript.Echo Now & " " & strLog
End Function



不足之处:
1 无法实现自动运行,需要添加到计划任务里或有其他的应用调用
2 目前未实现文件的删除操作
3 目前清理文件时无法实现对指定格式的文件进行清理,清理了所有文件
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics