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, objFile, objFSO, objScript
  5. Dim strBinFile, strFileExt, strFileName, strLine, strOut, strScriptFile, strScriptVer
  6.  
  7. strScriptVer = "2.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
  25. 			WScript.Echo "Binary input file not found" & vbCrLf
  26. 			Syntax
  27. 		End If
  28. 		With objFSO
  29. 			strScriptFile = .BuildPath( .GetParentFolderName( .GetAbsolutePathName( strBinFile ) ), .GetBaseName( strBinFile ) & ".vbs" )
  30. 		End With
  31. 		If objFSO.FileExists( strScriptFile ) Then
  32. 			WScript.Echo "Output script file already exists" & vbCrLf
  33. 			Syntax
  34. 		End If
  35. 	Else
  36. 		Syntax
  37. 	End If
  38. End With
  39.  
  40. Set objBinFile   = objFSO.GetFile( strBinFile )
  41. Set objBinStream = objBinFile.OpenAsTextStream( ForReading, TristateFalse )
  42. strFileName = objFSO.GetBaseName( strBinFile )
  43. strFileExt  = objFSO.GetExtensionName( strBinFile )
  44.  
  45. i = 0
  46. strOut = "Option Explicit" & vbCrLf _
  47.        & "Dim arrLine, i, objFile, objFSO, objParent, objShell" & vbCrLf _
  48.        & "Const ForWriting = 2" & vbCrLf _
  49.        & "Set objFSO  = CreateObject( ""Scripting.FileSystemObject"" )" & vbCrLf _
  50.        & "Set objFile = objFSO.CreateTextFile( """ & strFileName & "." & strFileExt & """, False, False )" & vbCrLf
  51. Do While Not objBinStream.AtEndOfStream
  52. 	i = i + 1
  53. 	If i = 1024 Then
  54. 		i = 0
  55. 		strOut  = strOut _
  56. 		        & "arrLine = Split( """ & Mid( strLine, 2 ) & """, "";"" )" & vbCrLf _
  57. 		        & "For i = 0 To UBound( arrLine )" & vbCrLf _
  58. 		        &  vbTab & "objFile.Write Chr( arrLine(i) )" & vbCrLf _
  59. 		        & "Next" & vbCrLf
  60. 		strLine = ""
  61. 	End If
  62. 	intByte = Asc( objBinStream.Read(1) )
  63. 	strLine = strLine & ";" & intByte
  64. Loop
  65. If strLine <> "" Then
  66. 	strOut  = strOut _
  67. 	        & "arrLine = Split( """ & Mid( strLine, 2 ) & """, "";"" )" & vbCrLf _
  68. 	        & "For i = 0 To UBound( arrLine )" & vbCrLf _
  69. 	        &  vbTab & "objFile.Write Chr( arrLine(i) )" & vbCrLf _
  70. 	        & "Next" & vbCrLf
  71. 	strLine = ""
  72. End If
  73. objBinStream.Close
  74. Set objBinStream = Nothing
  75. Set objBinFile   = Nothing
  76. strOut = strOut _
  77.        & "objFile.Close" & vbCrLf _
  78.        & "Set objShell  = CreateObject( ""Shell.Application"" )" & vbCrLf _
  79.        & "Set objParent = objShell.NameSpace( objFSO.GetParentFolderName( objFSO.GetAbsolutePathName( """ & strFileName & "." & strFileExt & """ ) ) )" & vbCrLf _
  80.        & "Set objFile   = objParent.ParseName( """ & objFSO.GetFileName( strFileName & "." & strFileExt ) & """ )" & vbCrLf _
  81.        & "objFile.ModifyDate  = """ & CStr( objFSO.GetFile( strBinFile ).DateLastModified ) & """" & vbCrLf _
  82.        & "Set objFile   = Nothing" & vbCrLf _
  83.        & "Set objFSO    = Nothing" & vbCrLf _
  84.        & "Set objShell  = Nothing" & vbCrLf _
  85.        & "WScript.Echo ""Created """"" & strFileName & "." & strFileExt & """""""" & vbCrLf
  86.  
  87. Set objScript = objFSO.CreateTextFile( strScriptFile, False, False )
  88. objScript.Write strOut
  89. objScript.Close
  90. Set objScript = Nothing
  91. Set objFSO    = Nothing
  92.  
  93.  
  94. Sub Syntax( )
  95. 	WScript.Echo "Bin2Vbs.vbs,  Version " & strScriptVer & vbCrLf _
  96. 	           & "Convert a (small) binary file to a script that can recreate that file" _
  97. 	           & vbCrLf & vbCrLf _
  98. 	           & "Usage:   BIN2VBS.VBS  binfile" _
  99. 	           & vbCrLf & vbCrLf _
  100. 	           & "Where:   ""binfile""    is the fully qualified path of the binary file" _
  101. 	           & vbCrLf & vbCrLf _
  102. 	           & "Result:  The VBScript file will be created in the binary file's parent folder," _
  103. 	           & vbCrLf _
  104. 	           & "         with the NAME of the binary file, and extension "".VBS""" _
  105. 	           & vbCrLf & vbCrLf _
  106. 	           & "Written by Rob van der Woude" & vbCrLf _
  107. 	           & "http://www.robvanderwoude.com"
  108. 	On Error Resume Next
  109. 	Set objBinFile   = Nothing
  110. 	Set objBinStream = Nothing
  111. 	Set objScript    = Nothing
  112. 	Set objFSO       = Nothing
  113. 	On Error GoTo 0
  114. 	WScript.Quit 1
  115. End Sub
  116.  

page last modified: 2024-04-16; loaded in 0.0064 seconds