(view source code of txt2vbs.vbs as plain text)
'Author: Denis St-Pierre (Ottawa, Canada)
'Purpose: Converts TXT files into VBS code that can recreate the TXT file
' The TXT file can even be another VBS file.
'History:
' 1.0 22oct2008 Initial release
'
'
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateUseDefault = -2
set WshShell = CreateObject( "WSCript.shell" )
'File browse dialog box
Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
objDialog.Filter = "All Files|*.*"
objDialog.InitialDir = WshShell.CurrentDirectory
intResult = objDialog.ShowOpen
If intResult = 0 Then
WshShell.Popup "No file selected.", 1, " ", 64
Wscript.Quit
Else
strFileNameIN = objDialog.FileName
End If
strFileNameOUT = strFileNameIN & "_CONVERTED.Vbs"
'Check if strFileNameOUT exists already
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
If objFSO.FileExists( strFileNameOUT ) then 'does the file EXIST?
' WScript.Echo "found"
OVRWT = MSGBOX( strFileNameOUT & " exists already" & vbCrLf & "Overwrite?", vbYesNoCancel, "Overwrite?" )
If OVRWT = 6 Then
'proceed
objFSO.DeleteFile( strFileNameOUT )
Else
WshShell.Popup "Exiting as requested.", 1, " ", 64
Wscript.Quit
End If
Else
' WScript.Echo "not found" 'strFileNameOUT does NOT exists already
End if
'open strFileNameANSI file and put entire file into a variable
Set objFile = objFSO.OpenTextFile( strFileNameIN, ForReading )
strText = objFile.ReadAll
objFile.Close
'Start converting
'Convert " to ""
strOldText = Chr(34)
strNewText = Chr(34) & Chr(34)
strText = Replace( strText, strOldText, strNewText )
'Add objTXTFile.writeline ("
strOldText = vbCrLf
strNewText = """)" & vbCrLf & vbTab & "objTXTFile.writeline ("""
strText = Replace( strText, strOldText, strNewText )
'Converting done
strFileName = objFSO.GetFileName( strFileNameIN )
'Write to file
Set objFile = objFSO.OpenTextFile( strFileNameOUT, ForAppending, True )
objFile.WriteLine "'this will create a file called " & strFileName & ".TXT"""
objFile.WriteLine "Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
objFile.WriteLine "strDP0=Replace(Ucase(Wscript.ScriptFullName), ""\""&Ucase(wscript.ScriptName), """")'path where this script is located"
objFile.WriteLine "strTXTfilename=strDP0&""\" & strFileName & ".TXT"""
objFile.WriteLine "set objTXTFile = objFSO.opentextfile(strTXTfilename, 8, true)"
objFile.WriteLine " objTXTFile.writeline (""" & strText & """)"
objFile.WriteLine "objTXTFile.close"
objFile.Close
WshShell.Popup "created " & strFileNameOUT, 3, "Completed", 64
page last modified: 2024-04-16; loaded in 0.0104 seconds