Thursday, December 22, 2011

VBScript : loadTextUnicode()

This function loads unicode text from textfiles. The vital part is the arguments we set when calling the fso.OpenTextFile function

EDIT: Just discovered a small bonus. If you set the fourth argument to "-2", you will be able to read both Unicode and ANSI files. When reading Microsoft's documentation it says that the value represents "System default".

I wonder..am i the only one that think MS's documentation on this seems incorrect?, look at the format values,..it says 2,1,0 it should it not have been -2,-1,0.?

Function loadTextUnicode(filePath)
 Dim fso, fto, resstr
 resstr = ""
 Set fso = CreateObject("Scripting.fileSystemObject")
 If fso.FileExists(filePath) Then
  if fso.GetFile(filePath).size > 0 Then 
   Set fto = fso.OpenTextFile(filePath, 1, 0, -2)
   resstr = fto.ReadAll
   fto.close()
  End If 
 Else
  wscript.echo(filePath + " did not exist")
 End If 
 set fso = nothing
 loadTextUnicode = resstr
End Function

'== EXAMPLE ==
myString = loadTextUnicode("myFile.txt")
wscript.echo myString

'

No comments:

Post a Comment