简单目录备份脚本

By | 2013/06/19

写这个脚本的原由是因为我经常会把一些文件放在桌面上,时间一长,桌面也就越来越乱了,自己在整理桌面的时候经常会不小心删除掉一些重要的文件,加上自己有清空回收站的强迫症,所以经常会造成一些悲剧。公司方面考虑到一些安全方面的问题,也不允许我们自己安装一些文件夹备份的软件,所以只好自己动手了~

脚本功能:

  • 将桌面上的文件备份到D:\Desktop-Backup\(D:\Desktop-Backup\这个目录只会增加新文件,并不会删除文件,所以这个目录会越来越大,需要手工进行清理)。
  • 会在D盘根目录生成两个文件:backup.tmp是临时文件,记录当前桌面上的所有文件;backup.log是日志文件,可以通过backup.log来查看已备份的文件(backup.tmp文件大小和你桌面上文件的多少有关;backup.log文件会越来越大,可以手工删除)。
  • 脚本程序每十分钟执行一次。
  • 如果更新过桌面上的文件,那么D:\Desktop-Backup\目录中对应的文件也会进行更新。

使用方法:

  • 将Desktop-backup.vbs文件拷贝到“开始菜单”中的“启动”目录下即可开机启动。
  • 如果想停止该脚本,则需要打开“任务管理器”,找到“wscript.exe”进程,点“结束进程”即可。

程序源码:

'FileName : Desktop-backup.vbs
'Author : moose
'Date : 2013/6/18
'Version : 0.2

'configure
Set ws = CreateObject("wscript.shell")
Set fs = CreateObject("scripting.filesystemobject")

SRCFLD = ws.SpecialFolders("desktop")
Const TARFLD = "D:\Desktop-Backup"	'target folder

If Not fs.FolderExists(TARFLD) Then
	fs.CreateFolder(TARFLD)
End If

TEMPFILE = "D:\backup.tmp"	'tmp text file .Why can not be const ?
LOGFILE = "D:\backup.log"    'log file

Set logfile = fs.OpenTextFile(LOGFILE, 8, True)
logfile.WriteLine "****************************"&now&"****************************"
logfile.WriteBlankLines 1
'start sync
Do While True
	If fs.FileExists(TEMPFILE) Then
		fs.DeleteFile TEMPFILE, True
	End If

	Set tmpFile = fs.OpenTextFile(TEMPFILE,8,True)
	fileTree(SRCFLD)
	tmpFile.Close

	syncFolder TEMPFILE,SRCFLD,TARFLD
	WScript.Sleep 1000*60*10    '10 minutes/backup
Loop

'delete tmpfile
'fs.DeleteFile TEMPFILE, True

'==================================================================

'sync folder
Function syncFolder(txtFile,srcPath,tarPath)
	'WScript.Echo TypeName(txtFile)  'if change TEMPFILE to TMPFILE than the type will be TextStream not string ! why ?
	'readline from tmp file
	Set dbFile = fs.OpenTextFile(txtFile,1)

	Do Until dbFile.AtEndOfStream
		sp = dbFile.ReadLine
		tp = Replace(sp,srcPath,tarPath)

		If Right(tp,1) = "\" Then
			'this record is folder, if not exist than create it
			If Not fs.FolderExists(tp) Then
				fs.CreateFolder(tp)
				'WScript.Echo "Create Folder :"&tp
				logfile.WriteLine Now & "  Create Folder :"&tp
			End If
		Else
			'this record is file
			If Not fs.FileExists(tp) Then
				'if file not exist
				fs.CopyFile sp, tp
				'WScript.Echo "Create File : "&tp
				logfile.WriteLine Now & "  Create File :"&tp
			Else
				'if file has been updated
				If fs.GetFile(sp).DateLastModified >fs.GetFile(tp).DateLastModified Then
					fs.CopyFile sp, tp, True
					'WScript.Echo "Replace File : "&tp
					logfile.WriteLine Now & "  Replace File :"&tp
				End If
			End If
		End If
	Loop
	dbFile.Close
End Function

'generate file tree and write file info to the tmp file
Function fileTree(folderPath)
	If Not fs.FolderExists(folderPath) Then
		WScript.Echo "Folder does not exists ~"
		WScript.Quit
	End If
	Set fld = fs.GetFolder(folderPath)

	'print files
	For Each file In fld.Files
		'WScript.Echo file
		tmpFile.WriteLine file.Path
	Next

	'print sub folders
	For Each subfld In fld.SubFolders
		'WScript.Echo subfld
		tmpFile.WriteLine subfld.Path&"\"
		fileTree(subfld)
	Next
End Function

 

PS:我一直纠结在是否要通过对比文件的MD5值来判断该文件是否要备份,但是因为现在的硬盘越来越廉价,加上MD5需要花费额外的时间,所以最后我采用对比文件的文件名和最后修改日期来判断文件是否需要备份。

 

Leave a Reply

Your email address will not be published. Required fields are marked *