DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Set a VBS file encoding  (Read 20,290 times)

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Set a VBS file encoding
« on: July 15, 2017, 04:32 PM »
Hello,

I use the following code in a BAT file to open a folder and select a file in an existing Windows Explorer window (QTTabBar installed).

Code
start "" %1

:VBSDynamicBuild
SET TempVBSFile=%temp%\~tmpSendKeysTemp.vbs

ECHO Set tSK = CreateObject("WScript.Shell") >> "%TempVBSFile%"
ECHO WScript.Sleep 200 >> "%TempVBSFile%"
ECHO tSK.SendKeys "^q%~2" >> "%TempVBSFile%"

CSCRIPT //NoLogo "%TempVBSFile%"

DEL /F /Q "%TempVBSFile%"

How can I change the temp VBS file encoding from the BAT file?

I'd appreciate your help.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Set a VBS file encoding
« Reply #1 on: July 15, 2017, 04:37 PM »
Code: ( vb)
    
Option Explicit

Private Const adReadAll = -1
Private Const adSaveCreateOverWrite = 2
Private Const adTypeBinary = 1
Private Const adTypeText = 2
Private Const adWriteChar = 0

Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName)
    Dim strText

    With CreateObject("ADODB.Stream")
        .Open
        .Type = adTypeBinary
        .LoadFromFile UTF8FName
        .Type = adTypeText
        .Charset = "utf-8"
        strText = .ReadText(adReadAll)
        .Position = 0
        .SetEOS
        .Charset = "_autodetect" 'Use current ANSI codepage.
        .WriteText strText, adWriteChar
        .SaveToFile ANSIFName, adSaveCreateOverWrite
        .Close
    End With
End Sub

UTF8toANSI "UTF8-wBOM.txt", "ANSI1.txt"
UTF8toANSI "UTF8-noBOM.txt", "ANSI2.txt"
MsgBox "Complete!", vbOKOnly, WScript.ScriptName

From https://stackoverflow.com/questions/5182102/vb6-vbscript-change-file-encoding-to-ansi

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Re: Set a VBS file encoding
« Reply #2 on: July 15, 2017, 07:48 PM »
Hello wraith808,

Thanks for replying. I appreciate it.

I've managed to convert from ANSI to UTF-8. Trying to convert from ANSI to Windows-1255 (Hebrew) does not work.
Any idea?

Best regards.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Set a VBS file encoding
« Reply #3 on: July 15, 2017, 08:04 PM »
So you changed

.Charset = "utf-8"

to what?

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Re: Set a VBS file encoding
« Reply #4 on: July 15, 2017, 08:11 PM »
I've changed .Charset = "utf-8" to .Charset = "_autodetect"
and then .Charset = "_autodetect" to .Charset = "Windows-1255".

Thanks again.
« Last Edit: July 15, 2017, 09:32 PM by Yaron »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Set a VBS file encoding
« Reply #5 on: July 15, 2017, 08:52 PM »
.Charset only works with type of adTypeText.  That could be the problem if you're trying to reverse it, as the open type is adTypeBinary.  You will probably have to play around a bit for your particular use case. 

The reference for ADODB.Stream can be found at https://www.w3schools.com/asp/ado_ref_stream.asp

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Re: Set a VBS file encoding
« Reply #6 on: July 15, 2017, 09:27 PM »
I'm not really familiar with encoding. I'll post here if the "playing around" works.

Thank you. I appreciate your help.

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Re: Set a VBS file encoding
« Reply #7 on: July 18, 2017, 03:51 PM »
SendKeys() does not support Unicode.

See http://www.donationcoder.com/forum/index.php?topic=44105.new#new for an alternative.

@wraith808,
Thanks again.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Set a VBS file encoding
« Reply #8 on: July 18, 2017, 09:39 PM »
SendKeys() does not support Unicode.

See http://www.donationcoder.com/forum/index.php?topic=44105.new#new for an alternative.

@wraith808,
Thanks again.

No problem!  Just sorry it didn't work; I didn't think about the fact that unicode could be the issue.

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
Re: Set a VBS file encoding
« Reply #9 on: July 18, 2017, 10:16 PM »
:up:

Original address: https://www.donationcoder.com/forum/index.php?topic=44086.0