Powered by GeSHi

Source code for txt2vbs.vbs

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

  1. 'Author: Denis St-Pierre (Ottawa, Canada)
  2. 'Purpose: Converts TXT files into VBS code that can recreate the TXT file
  3. '		  The TXT file can even be another VBS file.
  4.  
  5. 'History:
  6. '	1.0	22oct2008	Initial release
  7. '
  8. '
  9. Const ForReading         =  1
  10. Const ForWriting         =  2
  11. Const ForAppending       =  8
  12. Const TristateUseDefault = -2
  13.  
  14. set WshShell = CreateObject( "WSCript.shell" )
  15.  
  16. 'File browse dialog box
  17. Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
  18. objDialog.Filter     = "All Files|*.*"
  19. objDialog.InitialDir = WshShell.CurrentDirectory
  20. intResult = objDialog.ShowOpen
  21.  
  22. If intResult = 0 Then
  23. 	WshShell.Popup "No file selected.", 1, " ", 64
  24. 	Wscript.Quit
  25. Else
  26.     strFileNameIN = objDialog.FileName
  27. End If
  28.  
  29. strFileNameOUT = strFileNameIN & "_CONVERTED.Vbs"
  30.  
  31. 'Check if strFileNameOUT exists already
  32. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  33. If objFSO.FileExists( strFileNameOUT ) then  'does the file EXIST?
  34. '	WScript.Echo "found"
  35. 	OVRWT = MSGBOX( strFileNameOUT & " exists already" & vbCrLf & "Overwrite?", vbYesNoCancel, "Overwrite?" )
  36. 	If OVRWT = 6 Then
  37. 		'proceed
  38. 		objFSO.DeleteFile( strFileNameOUT )
  39. 	Else
  40. 		WshShell.Popup "Exiting as requested.", 1, " ", 64
  41. 		Wscript.Quit
  42. 	End If 
  43. Else
  44. '	WScript.Echo "not found" 'strFileNameOUT does NOT exists already
  45.  
  46. End if
  47.  
  48.  
  49. 'open strFileNameANSI file and put entire file into a variable
  50. Set objFile = objFSO.OpenTextFile( strFileNameIN, ForReading )
  51. strText = objFile.ReadAll
  52. objFile.Close
  53.  
  54. 'Start converting
  55.  
  56. 'Convert " to ""
  57. strOldText = Chr(34)
  58. strNewText = Chr(34) & Chr(34)
  59. strText    = Replace( strText, strOldText, strNewText )
  60.  
  61. 'Add objTXTFile.writeline ("
  62. strOldText = vbCrLf
  63. strNewText = """)" & vbCrLf & vbTab & "objTXTFile.writeline ("""
  64. strText    = Replace( strText, strOldText, strNewText )
  65. 'Converting done
  66.  
  67. strFileName = objFSO.GetFileName( strFileNameIN )
  68.  
  69. 'Write to file
  70. Set objFile = objFSO.OpenTextFile( strFileNameOUT, ForAppending, True )
  71. objFile.WriteLine "'this will create a file called " & strFileName & ".TXT"""
  72. objFile.WriteLine "Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
  73. objFile.WriteLine "strDP0=Replace(Ucase(Wscript.ScriptFullName), ""\""&Ucase(wscript.ScriptName), """")'path where this script is located"
  74. objFile.WriteLine "strTXTfilename=strDP0&""\" & strFileName & ".TXT"""
  75. objFile.WriteLine "set objTXTFile = objFSO.opentextfile(strTXTfilename, 8, true)"
  76. objFile.WriteLine "	objTXTFile.writeline (""" & strText & """)"
  77. objFile.WriteLine "objTXTFile.close"
  78. objFile.Close
  79.  
  80. WshShell.Popup "created " & strFileNameOUT, 3, "Completed", 64
  81.