Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bin2vbs.vbs

(view source code of bin2vbs.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim i, intByte
  4. Dim objBinFile, objBinStream, objFSO, objScript
  5. Dim strBinFile, strFileExt, strFileName, strLine, strOut, strScriptVer
  6.  
  7. strScriptVer = "1.00"
  8.  
  9. Const ForAppending = 8
  10. Const ForReading   = 1
  11. Const ForWriting   = 2
  12.  
  13. Const TristateFalse      =  0
  14. Const TristateMixed      = -2
  15. Const TristateTrue       = -1
  16. Const TristateUseDefault = -2
  17.  
  18. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  19.  
  20. With WScript.Arguments
  21. 	If .Named.Count   > 0 Then Syntax
  22. 	If .Unnamed.Count = 1 Then
  23. 		strBinFile = .Unnamed(0)
  24. 		If Not objFSO.FileExists( strBinFile ) Then Syntax
  25. 	Else
  26. 		Syntax
  27. 	End If
  28. End With
  29.  
  30. Set objBinFile   = objFSO.GetFile( strBinFile )
  31. Set objBinStream = objBinFile.OpenAsTextStream( ForReading, TristateFalse )
  32. strFileName = objFSO.GetBaseName( strBinFile )
  33. strFileExt  = objFSO.GetExtensionName( strBinFile )
  34.  
  35. i = 0
  36. strOut = "Option Explicit" & vbCrLf _
  37.        & "Dim objFile, objFSO" & vbCrLf _
  38.        & "Const ForWriting = 2" & vbCrLf _
  39.        & "Set objFSO = CreateObject( ""Scripting.FileSystemObject"" )" & vbCrLf _
  40.        & "Set objFile = objFSO.CreateTextFile( """ & strFileName & "." & strFileExt & """, False, False )" & vbCrLf
  41. Do While Not objBinStream.AtEndOfStream
  42. 	i = i + 1
  43. 	If i = 10 Then
  44. 		i = 0
  45. 		strOut  = strOut & "objFile.Write" & Mid( strLine, 3 ) & vbCrLf
  46. 		strLine = ""
  47. 	End If
  48. 	intByte = Asc( objBinStream.Read(1) )
  49. 	strLine = strLine & " & Chr(" & intByte & ")"
  50. Loop
  51. objBinStream.Close
  52. Set objBinStream = Nothing
  53. Set objBinFile   = Nothing
  54. If strLine <> "" Then strOut = strOut & "objFile.Write" & Mid( strLine, 3 ) & vbCrLf
  55. strOut = strOut _
  56.        & "objFile.Close" & vbCrLf _
  57.        & "Set objFile = Nothing" & vbCrLf _
  58.        & "Set objFSO = Nothing" & vbCrLf _
  59.        & "WScript.Echo ""Created """"" & strFileName & "." & strFileExt & """""""" & vbCrLf
  60.  
  61. Set objScript = objFSO.CreateTextFile( strFileName & ".vbs", False, False )
  62. objScript.Write strOut
  63. objScript.Close
  64. Set objScript = Nothing
  65. Set objFSO    = Nothing
  66.  
  67.  
  68. Sub Syntax( )
  69. ' Display help and, optionally, debugging information
  70. 	WScript.Echo "Bin2Vbs.vbs,  Version " & strScriptVer & vbCrLf _
  71. 	           & "Convert a (small) binary file to a script that can recreate that file" _
  72. 	           & vbCrLf & vbCrLf _
  73. 	           & "Usage:   BIN2VBS.VBS  binfile" _
  74. 	           & vbCrLf & vbCrLf _
  75. 	           & "Where:   ""binfile""    is the fully qualified path of the binary file" _
  76. 	           & vbCrLf & vbCrLf _
  77. 	           & "Result:  The VBScript file will be created in the current directory," _
  78. 	           & vbCrLf _
  79. 	           & "         with the NAME of the binary file, and extension "".VBS""" _
  80. 	           & vbCrLf & vbCrLf _
  81. 	           & "Written by Rob van der Woude" & vbCrLf _
  82. 	           & "http://www.robvanderwoude.com"
  83. 	WScript.Quit 1
  84. End Sub

page last uploaded: 2016-12-15, 11:20