Friday 11 April 2008

VBScript: Reading text files

Until now I have used the FileSystemObject (FSO) when reading text files in automation but this week I wanted to read a utf-8 encoded file which did not work using FSO. I used my standard approach dealing with this kind of problem:

1. Collect information about the issue (mainly using Google)
2. Analyze information and design solution
3. Implement solution

So I found another COM-object which can be used reading files which seems to manage most of the Char Sets I normally deal with: Adodb.Stream

The result (getFileContent.vbs):


Function getTextFileContent (strFileName, strCharSet)
Const adTypeBinary = 1 'not used
Const adTypeText = 2
'Set default CharSet
If strCharSet = "" Then strCharSet = "ASCII"
' *** CharSets ***
' Windows-1252
' Windows-1257
' UTF-16
' UTF-8
' UTF-7
' ASCII
' X-ANSI
' iso-8859-2
Set objStreamFile = CreateObject("Adodb.Stream")
With objStreamFile
.CharSet = strCharSet
.Type= adTypeText
.Open
.LoadFromFile strFileName
getTextFileContent = .readText
.Close
End With
Set objStreamFile = Nothing
End Function

8 comments:

Anonymous said...

Thank you! Checked a bunch of solutions and only yours helped.

Aniruddh Paralikar said...

Thanks a lot

This really helped me a lot !

Stefan Thelenius said...

Good to hear :-)

/Stefan

Carles Roch-Cunill said...

Thank you very much. It was really helpful and I was unable to find a good examples on the web.

Carles Roch-Cunill said...

Thank you very much. It was really helpful and I was unable to find a good examples on the web.

Wieland said...

Apparently still one of the better solutions! I only have one problem left: how can I put a html-break at the end of every line. I tried several possibilities but none works :-(
Any suggestions?

Stefan Thelenius said...

What are you trying to do?

Check out: http://www.w3schools.com/ado/met_stream_readtext.asp

Wieland said...

In ASP I try to read a txt-file. With .readText I lose the lay-out of my text. So after "soutenue." I want a 'break' (see example):

«Robe soutenue.
Un Saint-Estèphe de bon niveau, à attendre quelques années.»

resultitel=getTextFileContent(titeltext,char)

Function getTextFileContent(strFileName, strCharSet)
Const adTypeBinary = 1 'not used
Const adTypeText = 2
'Set default CharSet
If strCharSet = "" Then strCharSet = "Windows-1252"
Set objStreamFile = CreateObject("Adodb.Stream")

With objStreamFile
.CharSet = strCharSet
.Type= adTypeText
.Open
.LoadFromFile strFileName
Do Until objStreamFile.EOS
getTextFileContent = .readText(adReadLine)
Loop
.Close
End With