写这个脚本的原由是因为我经常会把一些文件放在桌面上,时间一长,桌面也就越来越乱了,自己在整理桌面的时候经常会不小心删除掉一些重要的文件,加上自己有清空回收站的强迫症,所以经常会造成一些悲剧。公司方面考虑到一些安全方面的问题,也不允许我们自己安装一些文件夹备份的软件,所以只好自己动手了~
脚本功能:
- 将桌面上的文件备份到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”进程,点“结束进程”即可。
1 'FileName : Desktop-backup.vbs 2 'Author : moose 3 'Date : 2013/6/18 4 'Version : 0.2 5 6 'configure 7 Set ws = CreateObject("wscript.shell") 8 Set fs = CreateObject("scripting.filesystemobject") 9 10 SRCFLD = ws.SpecialFolders("desktop") 11 Const TARFLD = "D:Desktop-Backup" 'target folder 12 13 If Not fs.FolderExists(TARFLD) Then 14 fs.CreateFolder(TARFLD) 15 End If 16 17 TEMPFILE = "D:ackup.tmp" 'tmp text file .Why can not be const ? 18 LOGFILE = "D:ackup.log" 'log file 19 20 Set logfile = fs.OpenTextFile(LOGFILE, 8, True) 21 logfile.WriteLine "****************************"&now&"****************************" 22 logfile.WriteBlankLines 1 23 'start sync 24 Do While True 25 If fs.FileExists(TEMPFILE) Then 26 fs.DeleteFile TEMPFILE, True 27 End If 28 29 Set tmpFile = fs.OpenTextFile(TEMPFILE,8,True) 30 fileTree(SRCFLD) 31 tmpFile.Close 32 33 syncFolder TEMPFILE,SRCFLD,TARFLD 34 WScript.Sleep 1000*60*10 '10 minutes/backup 35 Loop 36 37 'delete tmpfile 38 'fs.DeleteFile TEMPFILE, True 39 40 '================================================================== 41 42 'sync folder 43 Function syncFolder(txtFile,srcPath,tarPath) 44 'WScript.Echo TypeName(txtFile) 'if change TEMPFILE to TMPFILE than the type will be TextStream not string ! why ? 45 'readline from tmp file 46 Set dbFile = fs.OpenTextFile(txtFile,1) 47 48 Do Until dbFile.AtEndOfStream 49 sp = dbFile.ReadLine 50 tp = Replace(sp,srcPath,tarPath) 51 52 If Right(tp,1) = "" Then 53 'this record is folder, if not exist than create it 54 If Not fs.FolderExists(tp) Then 55 fs.CreateFolder(tp) 56 'WScript.Echo "Create Folder :"&tp 57 logfile.WriteLine Now & " Create Folder :"&tp 58 End If 59 Else 60 'this record is file 61 If Not fs.FileExists(tp) Then 62 'if file not exist 63 fs.CopyFile sp, tp 64 'WScript.Echo "Create File : "&tp 65 logfile.WriteLine Now & " Create File :"&tp 66 Else 67 'if file has been updated 68 If fs.GetFile(sp).DateLastModified >fs.GetFile(tp).DateLastModified Then 69 fs.CopyFile sp, tp, True 70 'WScript.Echo "Replace File : "&tp 71 logfile.WriteLine Now & " Replace File :"&tp 72 End If 73 End If 74 End If 75 Loop 76 dbFile.Close 77 End Function 78 79 'generate file tree and write file info to the tmp file 80 Function fileTree(folderPath) 81 If Not fs.FolderExists(folderPath) Then 82 WScript.Echo "Folder does not exists ~" 83 WScript.Quit 84 End If 85 Set fld = fs.GetFolder(folderPath) 86 87 'print files 88 For Each file In fld.Files 89 'WScript.Echo file 90 tmpFile.WriteLine file.Path 91 Next 92 93 'print sub folders 94 For Each subfld In fld.SubFolders 95 'WScript.Echo subfld 96 tmpFile.WriteLine subfld.Path&"" 97 fileTree(subfld) 98 Next 99 End Function
PS:我一直纠结在是否要通过对比文件的MD5值来判断该文件是否要备份,但是因为现在的硬盘越来越廉价,加上MD5需要花费额外的时间,所以最后我采用对比文件的文件名和最后修改日期来判断文件是否需要备份。