Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for txt2asci.vbs

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

  1. Option Explicit
  2.  
  3. Dim objFSO, strFileIn, strFileOut
  4.  
  5. With WScript.Arguments
  6. 	If .Named.Count > 0 Then
  7. 		Syntax
  8. 	End If
  9. 	Select Case .Unnamed.Count
  10. 		Case 1
  11. 			Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  12. 			strFileIn  = WScript.Arguments(0)
  13. 			strFileOut = objFSO.GetBaseName( strFileIn ) & "_ascii.txt"
  14. 			Set objFSO = Nothing
  15. 		Case 2
  16. 			strFileIn  = WScript.Arguments(0)
  17. 			strFileOut = WScript.Arguments(1)
  18. 		Case Else
  19. 			Syntax
  20. 	End Select
  21. End With
  22.  
  23. ASCII strFileIn, strFileOut
  24.  
  25.  
  26. Function ASCII( myFileIn, myFileOut )
  27. ' ASCII()  Version 1.00
  28. ' Open a "plain" text file and save it again in US ASCII format
  29. ' (overwriting an existing file without asking for confirmation).
  30. '
  31. ' Based on a sample script from JTMar:
  32. ' http://bytes.com/groups/asp/52959-save-file-utf-8-format-asp-vbscript
  33. '
  34. ' Written by Rob van der Woude
  35. ' http://www.robvanderwoude.com
  36.  
  37. 	Dim objStream, strText
  38.  
  39. 	' Valid Charset values for ADODB.Stream
  40. 	Const CdoBIG5        = "big5"
  41. 	Const CdoEUC_JP      = "euc-jp"
  42. 	Const CdoEUC_KR      = "euc-kr"
  43. 	Const CdoGB2312      = "gb2312"
  44. 	Const CdoISO_2022_JP = "iso-2022-jp"
  45. 	Const CdoISO_2022_KR = "iso-2022-kr"
  46. 	Const CdoISO_8859_1  = "iso-8859-1"
  47. 	Const CdoISO_8859_2  = "iso-8859-2"
  48. 	Const CdoISO_8859_3  = "iso-8859-3"
  49. 	Const CdoISO_8859_4  = "iso-8859-4"
  50. 	Const CdoISO_8859_5  = "iso-8859-5"
  51. 	Const CdoISO_8859_6  = "iso-8859-6"
  52. 	Const CdoISO_8859_7  = "iso-8859-7"
  53. 	Const CdoISO_8859_8  = "iso-8859-8"
  54. 	Const CdoISO_8859_9  = "iso-8859-9"
  55. 	Const cdoKOI8_R      = "koi8-r"
  56. 	Const cdoShift_JIS   = "shift-jis"
  57. 	Const CdoUS_ASCII    = "us-ascii"
  58. 	Const CdoUTF_7       = "utf-7"
  59. 	Const CdoUTF_8       = "utf-8"
  60.  
  61. 	' ADODB.Stream file I/O constants
  62. 	Const adTypeBinary          = 1
  63. 	Const adTypeText            = 2
  64. 	Const adSaveCreateNotExist  = 1
  65. 	Const adSaveCreateOverWrite = 2
  66.  
  67. 	On Error Resume Next
  68.  
  69. 	Set objStream = CreateObject( "ADODB.Stream" )
  70. 	objStream.Open
  71. 	objStream.Type = adTypeText
  72. 	objStream.Position = 0
  73. 	objStream.LoadFromFile myFileIn
  74. 	strText = objStream.ReadText
  75. 	objStream.Close
  76.  
  77. 	objStream.Open
  78. 	objStream.Type = adTypeText
  79. 	objStream.Position = 0
  80. 	objStream.Charset = CdoUS_ASCII
  81. 	objStream.WriteText strText
  82. 	objStream.SaveToFile myFileOut, adSaveCreateOverWrite
  83. 	objStream.Close
  84. 	Set objStream = Nothing
  85.  
  86. 	If Err Then
  87. 		ASCII = False
  88. 	Else
  89. 		ASCII = True
  90. 	End If
  91.  
  92. 	On Error Goto 0
  93. End Function
  94.  
  95.  
  96. Sub Syntax( )
  97. 	Dim strMsg
  98. 	strMsg = vbCrLf _
  99. 	       & "Txt2ASCI.vbs,  Version 1.00" & vbCrLf _
  100. 	       & "Convert any ""plain"" text file into US ASCII format" _
  101. 	       & vbCrLf & vbCrLf _
  102. 	       & "Usage:  TXT2ASCI.VBS  input_file  [ output_file ]" _
  103. 	       & vbCrLf & vbCrLf _
  104. 	       & "Where:  input_file   is the text file to be converted" _
  105. 	       & vbCrLf _
  106. 	       & "        output_file  is the ASCII text file to be created" _
  107. 	       & vbCrLf _
  108. 	       & "                     (default: input_file basename with ""_ascii.txt"" appended)" _
  109. 	       & vbCrLf & vbCrLf _	       
  110. 	       & "Notes:  If output_file exists, it will be overwritten without notification." _
  111. 	       & vbCrLf _
  112. 	       & "        This script has only minimal error handling, it does not check if" _
  113. 	       & vbcrlf _
  114. 	       & "        files exist." _
  115. 	       & vbCrLf & vbCrLf _
  116. 	       & "Written by Rob van der Woude" & vbCrLf _
  117. 	       & "http://www.robvanderwoude.com" & vbCrLf
  118. 	WScript.Echo strMsg
  119. 	WScript.Quit 1
  120. End Sub
  121.  

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