简单目录备份脚本

来源:互联网 发布:淘宝双11营业额2017 编辑:程序博客网 时间:2024/05/21 10:55

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

脚本功能:

  • 将桌面上的文件备份到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 folder12  13 If Not fs.FolderExists(TARFLD) Then14     fs.CreateFolder(TARFLD)15 End If16  17 TEMPFILE = "D:\backup.tmp"  'tmp text file .Why can not be const ?18 LOGFILE = "D:\backup.log"    'log file19  20 Set logfile = fs.OpenTextFile(LOGFILE, 8, True)21 logfile.WriteLine "****************************"&now&"****************************"22 logfile.WriteBlankLines 123 'start sync24 Do While True25     If fs.FileExists(TEMPFILE) Then26         fs.DeleteFile TEMPFILE, True27     End If28  29     Set tmpFile = fs.OpenTextFile(TEMPFILE,8,True)30     fileTree(SRCFLD)31     tmpFile.Close32  33     syncFolder TEMPFILE,SRCFLD,TARFLD34     WScript.Sleep 1000*60*10    '10 minutes/backup35 Loop36  37 'delete tmpfile38 'fs.DeleteFile TEMPFILE, True39  40 '==================================================================41  42 'sync folder43 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 file46     Set dbFile = fs.OpenTextFile(txtFile,1)47  48     Do Until dbFile.AtEndOfStream49         sp = dbFile.ReadLine50         tp = Replace(sp,srcPath,tarPath)51  52         If Right(tp,1) = "\" Then53             'this record is folder, if not exist than create it54             If Not fs.FolderExists(tp) Then55                 fs.CreateFolder(tp)56                 'WScript.Echo "Create Folder :"&tp57                 logfile.WriteLine Now & "  Create Folder :"&tp58             End If59         Else60             'this record is file61             If Not fs.FileExists(tp) Then62                 'if file not exist63                 fs.CopyFile sp, tp64                 'WScript.Echo "Create File : "&tp65                 logfile.WriteLine Now & "  Create File :"&tp66             Else67                 'if file has been updated68                 If fs.GetFile(sp).DateLastModified >fs.GetFile(tp).DateLastModified Then69                     fs.CopyFile sp, tp, True70                     'WScript.Echo "Replace File : "&tp71                     logfile.WriteLine Now & "  Replace File :"&tp72                 End If73             End If74         End If75     Loop76     dbFile.Close77 End Function78  79 'generate file tree and write file info to the tmp file80 Function fileTree(folderPath)81     If Not fs.FolderExists(folderPath) Then82         WScript.Echo "Folder does not exists ~"83         WScript.Quit84     End If85     Set fld = fs.GetFolder(folderPath)86  87     'print files88     For Each file In fld.Files89         'WScript.Echo file90         tmpFile.WriteLine file.Path91     Next92  93     'print sub folders94     For Each subfld In fld.SubFolders95         'WScript.Echo subfld96         tmpFile.WriteLine subfld.Path&"\"97         fileTree(subfld)98     Next99 End Function

 

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

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>