找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 17370|回复: 2

[技术讨论] [分享]VBS以TXT备份还原文件

[复制链接]
发表于 2011-11-9 23:50:44 | 显示全部楼层 |阅读模式
把任意格式的文件分解到文本文件中,并且还可以用此脚本进行恢复...
目前把目标文件限制为 1M 以内,因为大的文件会另保存的文本文件变得很大...
使用方法:把需要处理的文件拖曳到此脚本上就可以了...

  1. Option Explicit

  2. Dim blnNum, objFso, objFile, strFile, lngCnt, lngStartTime

  3. If WScript.Arguments.Count = 0 Then
  4.         CreateObject("Wscript.Shell").Popup "把文件拖到我身上就行了", 3, "^o^", 0
  5.         WScript.Quit
  6. End If

  7. lngStartTime = Timer

  8. strFile = WScript.Arguments(0)
  9. Set objFso = CreateObject("Scripting.FileSystemObject")


  10. '检查文件名后面部分是否为备份的 TXT 文件
  11. If RM(strFile, "^.+_back\.txt$", True, True) = True Then
  12.         Set objFile = objFso.OpenTextFile(strFile, 1, False)
  13.         lngCnt = 0
  14.         blnNum = False
  15.         Do Until objFile.AtEndOfStream
  16.                 '检查数值范围是否为0~255(byte)
  17.                 blnNum = RM(objFile.ReadLine, _
  18.                         "^(?:1\d\d|2[0-4]\d|[1-9]\d|25[0-5]|\d)$", True, True)
  19.                 If blnNum = False Then Exit Do
  20.                 lngCnt = lngCnt + 1
  21.                 If lngCnt > 1000 Then Exit Do        '最多只检查前 1000 行数据
  22.         Loop
  23.         objFile.Close
  24.         Set objFile = Nothing
  25.        
  26.         If blnNum = False Then
  27.                 set objFso = Nothing
  28.                 MsgBox "文件内容不符合备份文件的格式", vbOKOnly, ":("
  29.                 WScript.Quit
  30.         End If
  31.        
  32.         '如果已经存在原文件则提示是否覆盖
  33.         If objFso.FileExists(Left(strFile, len(strfile) - 9)) Then
  34.                 If MsgBox("目标文件已经存在,是否覆盖???", vbOKCancel, _
  35.                         "确定:覆盖   取消:退出") = 1 Then
  36.                         lngStartTime = Timer        '重新计时
  37.                         Call Recovery(strFile)
  38.                 End If
  39.         Else
  40.                 Call Recovery(strFile)
  41.         End If
  42. Else        '如果不是备份的 TXT 文件
  43.         If (objFso.GetFile(strFile).Size \ 1024) > 1024 Then
  44.                 '检查文件大小,大的文件处理的时间太长
  45.                 Set objFso = Nothing
  46.                 MsgBox "对不起,目前暂不处理大于 1M 的文件", vbOKOnly, ":("
  47.                 WScript.Quit
  48.         End if
  49.         If objFso.FileExists(strFile & "_back.txt") Then
  50.                 '如果存在备份文件则提示是否覆盖
  51.                 If MsgBox("备份文件已经存在,是否覆盖???", vbOKCancel, _
  52.                         "确定:覆盖   取消:退出") = 1 Then
  53.                         lngStartTime = Timer        '重新计时
  54.                         Call Backup(strFile)
  55.                 End If
  56.         Else
  57.                 Call Backup(strFile)
  58.         End If
  59. End If

  60. Set objFso = Nothing
  61. CreateObject("Wscript.Shell").Popup "耗时:【" & _
  62.         Round(Timer - lngStartTime, 4) & "】秒", 5, "Done...", 0


  63. '##############################################################################
  64. Sub Recovery(srcFile)        '把保存为 TXT 的文件恢复为原文件
  65.         Dim arrBit(), objFso, objFile, objADODB, lngCnt, strTmp, arrChr, i
  66.         lngCnt = 0
  67.        
  68.         Set objFso = CreateObject("Scripting.FileSystemObject")
  69.         Set objFile = objFso.OpenTextFile(srcFile, 1, False)
  70.         Set objADODB = CreateObject("ADODB.Stream")
  71.         Do Until objFile.AtEndOfStream
  72.                 ReDim Preserve arrBit(lngCnt)
  73.                 arrBit(lngCnt) = objFile.ReadLine
  74.                 lngCnt = lngCnt + 1
  75.         Loop
  76.         objFile.Close
  77.         Set objFile = Nothing
  78.         Set objFso = Nothing
  79.        
  80.         lngCnt = lngCnt - 1
  81.         ReDim arrChr(lngCnt \ 2)
  82.         For i = 0 To lngCnt - 1 Step 2
  83.                 arrChr(i \ 2) = ChrW(arrBit(i + 1) * 256 + arrBit(i))
  84.         Next
  85.         If i = lngCnt Then arrChr(i \ 2) = ChrW(arrBit(i))
  86.         arrChr = Join(arrChr, "")
  87.         objADODB.Type = 1
  88.         objADODB.Open
  89.         With CreateObject("ADODB.Stream")
  90.                 .Type = 2        'adTypeText = 2
  91.                 .Open
  92.                 .Writetext arrChr
  93.                 .Position =  2
  94.                 .Copyto objADODB
  95.                 .Close
  96.         End With
  97.         objADODB.SaveToFile Left(srcFile, len(srcfile) - 9), 2
  98.                 'adSaveCreateOverWrite = 2, adSaveCreateNotExist = 1
  99.         objADODB.Close
  100.         Set objADODB = Nothing
  101. End Sub


  102. '##############################################################################
  103. Sub Backup(srcFile)                '把源文件存储为 TXT 文件
  104.         Dim objADODB, objFso, objFl, i, arrBit(0)
  105.         Set objADODB = CreateObject("ADODB.Stream")
  106.         Set objFso = CreateObject("Scripting.FileSystemObject")
  107.         Set objFl = objFso.OpenTextFile(srcFile & "_Back.txt", 2, True)
  108.         With objADODB
  109.                 .Open
  110.                 .Type = 1        'adTypeBinary = 1
  111.                 .LoadFromFile srcFile
  112.                 For i = 0 To .Size - 1
  113.                         arrBit(0) = AscB(.Read(1))
  114.                         objFl.WriteLine arrBit(0)
  115.                 Next
  116.                 .Close
  117.         End With
  118.         Set objADODB = Nothing
  119.         objFl.Close
  120.         Set objFl = Nothing
  121.         Set objFso = Nothing
  122. End Sub


  123. '##############################################################################
  124. Function RM(strVar, strPtrn, blnGlb, blnCase)
  125.         'Regular-expression Match
  126.         RM = False
  127.         Dim objReg
  128.         Set objReg = New RegExp
  129.         With objReg
  130.                 .Pattern = strPtrn
  131.                 .Global = blnGlb
  132.                 .IgnoreCase = blnCase
  133.                 RM = .Test(strVar)
  134.         End With
  135.         Set objReg = Nothing
  136. End Function
复制代码

评分

参与人数 1技术 +1 收起 理由
wc726842270 + 1 学习

查看全部评分

 楼主| 发表于 2011-11-10 00:20:49 | 显示全部楼层
应用: 把GREP.EXE还原到临时文件夹中并用GREP.EXE打印脚本中非注释部分的代码...
由于文件比较大,故只贴出还原的代码,全部代码(包含GREP.EXE的内容)则以附件形式上传...

  1. Option Explicit

  2. '把保存为 TXT 的文件恢复为原文件
  3. Dim arrBit(), arrChr, objFso, objFile, objADODB, lngCnt, strFile, i
  4. Dim blnStart, strTmp

  5. blnStart = False
  6. lngCnt = 0

  7. Set objFso = CreateObject("Scripting.FileSystemObject")
  8. Set objFile = objFso.OpenTextFile(WScript.ScriptFullName, 1, False)
  9. Set objADODB = CreateObject("ADODB.Stream")
  10. strFile = objFso.GetSpecialFolder(2) & "\grep.exe"        '文件还原到"临时文件夹"

  11. '把文件内容赋值给数组
  12. Do Until objFile.AtEndOfStream
  13.         If blnStart = True Then
  14.                 ReDim Preserve arrBit(lngCnt)
  15.                 strTmp = objFile.ReadLine
  16.                 arrBit(lngCnt) = Right(strTmp, Len(strTmp) - 1)
  17.                 lngCnt = lngCnt + 1
  18.         Else
  19.                 If objFile.ReadLine = "'grep.exe" Then blnStart = True
  20.         End If
  21. Loop
  22. objFile.Close
  23. Set objFile = Nothing

  24. '还原文件
  25. lngCnt = lngCnt - 1
  26. ReDim arrChr(lngCnt \ 2)
  27. For i = 0 To lngCnt - 1 Step 2
  28.         arrChr(i \ 2) = ChrW(arrBit(i + 1) * 256 + arrBit(i))
  29. Next
  30. If i = lngCnt Then arrChr(i \ 2) = ChrW(arrBit(i))
  31. arrChr = Join(arrChr, "")
  32. objADODB.Type = 1
  33. objADODB.Open
  34. With CreateObject("ADODB.Stream")
  35.         .Type = 2
  36.         .Open
  37.         .Writetext arrChr
  38.         .Position =  2
  39.         .Copyto objADODB
  40.         .Close
  41. End With
  42. objADODB.SaveToFile strFile, 2
  43. objADODB.Close
  44. Set objADODB = Nothing
  45. Set objFso = Nothing

  46. CreateObject("Wscript.Shell").Run "cmd /c " & strFile & " -P ""^[^']+"" """ & _
  47.         WScript.ScriptFullName & """&echo.&set/p=请按任意键退出...<nul&pause>nul"

复制代码
http://pan.baidu.com/share/link?shareid=164262390&uk=1124163200
 楼主| 发表于 2011-11-26 19:35:14 | 显示全部楼层
再来一个还原嵌入mp3铃声的代码...

  1. Option Explicit

  2. '把保存为 TXT 的文件恢复为原文件
  3. Dim arrBit(), arrChr, objFso, objFile, objADODB, lngCnt, strFile, i
  4. Dim blnStart, strTmp

  5. blnStart = False
  6. lngCnt = 0

  7. Set objFso = CreateObject("Scripting.FileSystemObject")
  8. strFile = objFso.GetSpecialFolder(2) & "\不要用我的爱来伤害我.mp3"        '文件还原到"临时文件夹"

  9. '如果已经存在指定文件(非第一次运行此脚本)则直接调用播放的过程
  10. If objFso.FileExists(strFile) Then
  11.         Call PlaySong(strFile)
  12.         Set objFso = Nothing
  13.         WScript.Quit
  14. End If

  15. Set objFile = objFso.OpenTextFile(WScript.ScriptFullName, 1, False)
  16. Set objADODB = CreateObject("ADODB.Stream")

  17. '把文件内容赋值给数组
  18. Do Until objFile.AtEndOfStream
  19.         If blnStart = True Then
  20.                 ReDim Preserve arrBit(lngCnt)
  21.                 strTmp = objFile.ReadLine
  22.                 arrBit(lngCnt) = Right(strTmp, Len(strTmp) - 1)
  23.                 lngCnt = lngCnt + 1
  24.         Else
  25.                 If objFile.ReadLine = "'不要用我的爱来伤害我.mp3" Then blnStart = True
  26.         End If
  27. Loop
  28. objFile.Close
  29. Set objFile = Nothing

  30. '还原文件
  31. lngCnt = lngCnt - 1
  32. ReDim arrChr(lngCnt \ 2)
  33. For i = 0 To lngCnt - 1 Step 2
  34.         arrChr(i \ 2) = ChrW(arrBit(i + 1) * 256 + arrBit(i))
  35. Next
  36. If i = lngCnt Then arrChr(i \ 2) = ChrW(arrBit(i))
  37. arrChr = Join(arrChr, "")
  38. objADODB.Type = 1
  39. objADODB.Open
  40. With CreateObject("ADODB.Stream")
  41.         .Type = 2
  42.         .Open
  43.         .Writetext arrChr
  44.         .Position =  2
  45.         .Copyto objADODB
  46.         .Close
  47. End With
  48. objADODB.SaveToFile strFile, 2
  49. objADODB.Close
  50. Set objADODB = Nothing
  51. Set objFso = Nothing

  52. Call PlaySong(strFile)

  53. Sub PlaySong(strMusic)
  54.         Dim i
  55.         For i = 0 To 2        '播放三次
  56.                 With CreateObject("WMPlayer.ocx")
  57.                         .url = strMusic
  58.                         .controls.play
  59.                         Do Until .playstate = 1
  60.                                 WScript.Sleep 500
  61.                         Loop
  62.                 End With
  63.         Next
  64. End Sub

  65. '不要用我的爱来伤害我.mp3
复制代码
http://pan.baidu.com/share/link?shareid=159314051&uk=1124163200
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 07:31 , Processed in 0.019390 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表