topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 8:10 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Set a VBS file encoding  (Read 13641 times)

Yaron

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Set a VBS file encoding
« on: July 15, 2017, 11:32 AM »
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).

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
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #1 on: July 15, 2017, 11:37 AM »
Code: Visual Basic [Select]
  1. Option Explicit
  2.  
  3. Private Const adReadAll = -1
  4. Private Const adSaveCreateOverWrite = 2
  5. Private Const adTypeBinary = 1
  6. Private Const adTypeText = 2
  7. Private Const adWriteChar = 0
  8.  
  9. Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName)
  10.     Dim strText
  11.  
  12.     With CreateObject("ADODB.Stream")
  13.         .Open
  14.         .Type = adTypeBinary
  15.         .LoadFromFile UTF8FName
  16.         .Type = adTypeText
  17.         .Charset = "utf-8"
  18.         strText = .ReadText(adReadAll)
  19.         .Position = 0
  20.         .SetEOS
  21.         .Charset = "_autodetect" 'Use current ANSI codepage.
  22.        .WriteText strText, adWriteChar
  23.         .SaveToFile ANSIFName, adSaveCreateOverWrite
  24.         .Close
  25.     End With
  26. End Sub
  27.  
  28. UTF8toANSI "UTF8-wBOM.txt", "ANSI1.txt"
  29. UTF8toANSI "UTF8-noBOM.txt", "ANSI2.txt"
  30. MsgBox "Complete!", vbOKOnly, WScript.ScriptName

From https://stackoverflo...ile-encoding-to-ansi

Yaron

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #2 on: July 15, 2017, 02: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
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #3 on: July 15, 2017, 03:04 PM »
So you changed

.Charset = "utf-8"

to what?

Yaron

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #4 on: July 15, 2017, 03: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, 04:32 PM by Yaron »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #5 on: July 15, 2017, 03: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.w3school...p/ado_ref_stream.asp

Yaron

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #6 on: July 15, 2017, 04: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
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #7 on: July 18, 2017, 10:51 AM »
SendKeys() does not support Unicode.

See https://www.donation...?topic=44105.new#new for an alternative.

@wraith808,
Thanks again.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #8 on: July 18, 2017, 04:39 PM »
SendKeys() does not support Unicode.

See https://www.donation...?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
  • *
  • default avatar
  • Posts: 22
    • View Profile
    • Donate to Member
Re: Set a VBS file encoding
« Reply #9 on: July 18, 2017, 05:16 PM »
 :up: