[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

RE: VBS如何以二进制形式输出一个exe文件

本帖最后由 more 于 2016-9-18 09:53 编辑

这是我以前写的一个还原铃声的代码,可以借鉴一下:
  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
  66. '255
  67. '250
  68. '179
复制代码
'由于 2M 的铃声文件(txt后缀)提示文件过大而无法上传,遗憾

'分隔符#######################
'这个函数可以把文件以二进制分解后存储为 txt 后缀
  1. Sub Backup(srcFile)
  2.     Dim objADODB, objFso, objFl, i, arrBit(0)
  3.     Set objADODB = CreateObject("ADODB.Stream")
  4.     Set objFso = CreateObject("Scripting.FileSystemObject")
  5.     Set objFl = objFso.OpenTextFile(srcFile & "_Back.txt", 2, True)
  6.     With objADODB
  7.         .Open
  8.         .Type = 1    'adTypeBinary = 1
  9.         .LoadFromFile srcFile
  10.         For i = 0 To .Size - 1
  11.             arrBit(0) = AscB(.Read(1))
  12.             objFl.WriteLine arrBit(0)
  13.         Next
  14.         .Close
  15.     End With
  16.     Set objADODB = Nothing
  17.     objFl.Close
  18.     Set objFl = Nothing
  19.     Set objFso = Nothing
  20. End Sub
复制代码

TOP

返回列表