dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Encoding in FileSystemObject

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    188

    Resolved [RESOLVED] Encoding in FileSystemObject

    Hi guys!

    Another question regarding FileSystemObject. I am playing around with this object and facing a number of problems, therefore I have more questions for this object.

    Now, I have created a few text files which contain unicode characters by using FileSystemObject's CreateTextFile method. Once the text file is saved, the encoding is saved as UCS-2 Little Endian.

    So, my question is, can i set the encoding I want? I need the files to be saved as UTF-8 encoding.

    Please help!

    Thanks!
    OB

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Encoding in FileSystemObject

    Quote Originally Posted by oceanboy
    So, my question is, can i set the encoding I want? I need the files to be saved as UTF-8 encoding.
    UTF-8 is a superset of ASCII and therefore requires no addition conversion.
    However, you may "convert" your text to ascii codes using byte array. Have a look at this sample.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,869

    Re: Encoding in FileSystemObject

    The FSO is limited to "ANSI" (which in Windows-speak really means "UTF-16LE converted to 8-bit DOS encoding using the current codepage") and UTF-16LE.

    If your files are not gigantic (< ~20MB) you can probably do the conversion (or create them in the first place) using the ADODB.Stream object. Things get only slightly tricky for Unicode encodings when you want to omit the BOM, but that is shown in the demo as well.

    Generally you'd be better off just creating the text files using ADODB.Stream in the first place instead of converting afterward. The FSO is very limited, and really meant only for Windows Scripting.


    The demo attached shows how to convert between various encodings. The encodings available can be found under:

    HKEY_CLASSES_ROOT\MIME\Database\Charset
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,869

    Re: Encoding in FileSystemObject

    Quote Originally Posted by RhinoBull
    UTF-8 is a superset of ASCII and therefore requires no addition conversion.
    Not strictly true, although if your locale is US English it is close enough to true to work 99.99% of the time. That 0.01% can kill you though.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,869

    Re: Encoding in FileSystemObject

    Quote Originally Posted by RhinoBull
    However, you may "convert" your text to ascii codes using byte array. Have a look at this sample.
    Horribly flawed example, subject to the same problems as assuming that UTF-8 = ASCII, because it makes that same assumption. Fails even worse than using StrConv() because it is locale-naive.

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Encoding in FileSystemObject

    You know - that is just an example of how things can be done, basically a heads up and rest is up to the coder.
    Whether sample code is horrible or not isn't for you to judge since you did not provide anything at all.

    And btw you could've combined two of your replies into one.


    Sorry.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,869

    Re: Encoding in FileSystemObject

    Last I looked I'd provided a complete, working, correct example.

    See ASCII and UTF-8 Table. I stand by my remarks.
    Last edited by dilettante; Feb 25th, 2009 at 06:14 PM.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,738

    Re: Encoding in FileSystemObject

    Even if an alternative example had not been provided, I think it is reasonable to point out issues - as the OP then knows what area(s) of the code might (depending on their circumstances) need attention.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    188

    Re: Encoding in FileSystemObject

    Hi guys,

    ADO Stream works fine!!

    It's ashame that FSO doesnt support UTF8, otherwise it is quite a handy object.

    Thanks all!

    OB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width