Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for b64coder.vbs

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

  1. Option Explicit
  2.  
  3. Dim errResult
  4.  
  5. WScript.Echo "Base64 encoding . . ."
  6. errResult = Base64Encode( "b64coder.vbs", "b64coder.enc" )
  7. If errResult <> 0 Then
  8. 	ShowError errResult
  9. End If
  10.  
  11. WScript.Echo "Decoding again . . ."
  12. errResult = Base64Decode( "b64coder.enc", "b64coder.dec" )
  13. If errResult <> 0 Then
  14. 	ShowError errResult
  15. Else
  16. 	WScript.Echo "Done." & vbCrLf _
  17. 	           & "Compare the files ""b64coder.vbs"" and " _
  18. 	           & """b64coder.dec"", they should be identical."
  19. End If
  20.  
  21.  
  22. Sub ShowError( myError )
  23. 	On Error Resume Next
  24. 	Err.Raise myError
  25. 	WScript.Echo "ERROR " & Err.Number & ": " & Err.Description
  26. 	Err.Clear
  27. 	On Error Goto 0
  28. 	WScript.Quit
  29. End Sub
  30.  
  31.  
  32. Function Base64Encode( myFileIn, myFileOut )
  33. ' This function uses Belus Technology's XBase64 component to Base64 encode a file.
  34. ' The XBase64 component is available at
  35. ' http://www.xstandard.com/page.asp?p=F5096C20-DF8F-4FAA-A1A5-CC85A934139A
  36. '
  37. ' Arguments:
  38. ' myFileIn   [string]  the file to be encoded
  39. ' myFileOut  [string]  the encoded file to be created
  40. '
  41. ' Return Code:
  42. ' 0 if all goes well, otherwise the appropriate error number
  43. '
  44. ' Written by Rob van der Woude
  45. ' http://www.robvanderwoude.com
  46.  
  47. 	' Standard housekeeping
  48. 	Dim i, objBase64, objFSO, objFileIn, objFileOut
  49. 	Dim objStreamIn, strCode, strText
  50.  
  51. 	Const ForAppending       =  8
  52. 	Const ForReading         =  1
  53. 	Const ForWriting         =  2
  54. 	Const TristateFalse      =  0
  55. 	Const TristateMixed      = -2
  56. 	Const TristateTrue       = -1
  57. 	Const TristateUseDefault = -2
  58. 	Const otSafeArray        =  0
  59. 	Const otString           =  2
  60.  
  61. 	' Use custom error handling
  62. 	'On Error Resume Next
  63.  
  64. 	' Open a file system object
  65. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  66.  
  67. 	' Open the input file if it exists
  68. 	If objFSO.FileExists( myFileIn ) Then
  69. 		Set objFileIn   = objFSO.GetFile( myFileIn )
  70. 		Set objStreamIn = objFileIn.OpenAsTextStream( ForReading, TriStateFalse )
  71. 	Else
  72. 		' Error 53: File not found
  73. 		Base64Encode = 53
  74. 		' Close input file and release objects
  75. 		objStreamIn.Close
  76. 		Set objStreamIn = Nothing
  77. 		Set objFileIn   = Nothing
  78. 		Set objFSO      = Nothing
  79. 		' Abort
  80. 		Exit Function
  81. 	End If
  82.  
  83. 	' Create the output file, unless it already exists
  84. 	If objFSO.FileExists( myFileOut ) Then
  85. 		' Error 58: File already exists
  86. 		Base64Encode = 58
  87. 		' Close input file and release objects
  88. 		objStreamIn.Close
  89. 		Set objStreamIn = Nothing
  90. 		Set objFileIn   = Nothing
  91. 		Set objFSO      = Nothing
  92. 		' Abort
  93. 		Exit Function
  94. 	Else
  95. 		Set objFileOut = objFSO.CreateTextFile( myFileOut, True, False )
  96. 	End If
  97.  
  98. 	' Read the text from the input file and close the file
  99. 	strText = objStreamIn.ReadAll( )
  100. 	objStreamIn.Close
  101.  
  102. 	' Base64 encode the text stream
  103. 	Set objBase64 = CreateObject( "XStandard.Base64" )
  104. 	strCode       = objBase64.Encode( strText, otString )
  105. 	Set objBase64 = Nothing
  106.  
  107. 	' Write the result to the output file and close the file
  108. 	objFileOut.Write strCode
  109. 	objFileOut.Close
  110.  
  111. 	' Release the objects
  112. 	Set objStreamIn = Nothing
  113. 	Set objFileIn   = Nothing
  114. 	Set objFileOut  = Nothing
  115. 	Set objFSO      = Nothing
  116.  
  117. 	' Return any error codes
  118. 	Base64Encode = Err.Number
  119. 	On Error Goto 0
  120. End Function
  121.  
  122.  
  123. Function Base64Decode( myFileIn, myFileOut )
  124. ' This function uses Belus Technology's XBase64 component to Base64 decode a file.
  125. ' The XBase64 component is available at
  126. ' http://www.xstandard.com/page.asp?p=F5096C20-DF8F-4FAA-A1A5-CC85A934139A
  127. '
  128. ' Arguments:
  129. ' myFileIn   [string]  the file to be decoded
  130. ' myFileOut  [string]  the decoded file to be created
  131. '
  132. ' Return Code:
  133. ' 0 if all goes well, otherwise the appropriate error number
  134. '
  135. ' Written by Rob van der Woude
  136. ' http://www.robvanderwoude.com
  137.  
  138. 	' Standard housekeeping
  139. 	Dim i, objBase64, objFSO, objFileIn, objFileOut
  140. 	Dim objStreamIn, strCode, strText
  141.  
  142. 	Const ForAppending       =  8
  143. 	Const ForReading         =  1
  144. 	Const ForWriting         =  2
  145. 	Const TristateFalse      =  0
  146. 	Const TristateMixed      = -2
  147. 	Const TristateTrue       = -1
  148. 	Const TristateUseDefault = -2
  149. 	Const otSafeArray        =  0
  150. 	Const otString           =  2
  151.  
  152. 	' Use custom error handling
  153. 	'On Error Resume Next
  154.  
  155. 	' Open a file system object
  156. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  157.  
  158. 	' Open the input file if it exists
  159. 	If objFSO.FileExists( myFileIn ) Then
  160. 		Set objFileIn   = objFSO.GetFile( myFileIn )
  161. 		Set objStreamIn = objFileIn.OpenAsTextStream( ForReading, TriStateFalse )
  162. 	Else
  163. 		' Error 53: File not found
  164. 		Base64Decode = 53
  165. 		' Close input file and release objects
  166. 		objStreamIn.Close
  167. 		Set objStreamIn = Nothing
  168. 		Set objFileIn   = Nothing
  169. 		Set objFSO      = Nothing
  170. 		' Abort
  171. 		Exit Function
  172. 	End If
  173.  
  174. 	' Create the output file, unless it already exists
  175. 	If objFSO.FileExists( myFileOut ) Then
  176. 		' Error 58: File already exists
  177. 		Base64Decode = 58
  178. 		' Close input file and release objects
  179. 		objStreamIn.Close
  180. 		Set objStreamIn = Nothing
  181. 		Set objFileIn   = Nothing
  182. 		Set objFSO      = Nothing
  183. 		' Abort
  184. 		Exit Function
  185. 	Else
  186. 		Set objFileOut = objFSO.CreateTextFile( myFileOut, True, False )
  187. 	End If
  188.  
  189. 	' Read the encoded text from the input file and close the file
  190. 	strCode = objStreamIn.ReadAll( )
  191. 	objStreamIn.Close
  192.  
  193. 	' Base64 decode the text stream
  194. 	Set objBase64 = CreateObject( "XStandard.Base64" )
  195. 	strText       = objBase64.Decode( strCode, otString )
  196. 	Set objBase64 = Nothing
  197.  
  198. 	' Write the result to the output file and close the file
  199. 	objFileOut.Write strText
  200. 	objFileOut.Close
  201.  
  202. 	' Release the objects
  203. 	Set objStreamIn = Nothing
  204. 	Set objFileIn   = Nothing
  205. 	Set objFileOut  = Nothing
  206. 	Set objFSO      = Nothing
  207.  
  208. 	' Return any error codes
  209. 	Base64Decode = Err.Number
  210. 	On Error Goto 0
  211. End Function
  212.  

page last uploaded: 2017-04-06, 13:33