I have a csv file on the server encoded as ANSI, and want to open it, process and save the content on the database.
I'm having infinite problems with the accented character as "à è ì ò ù", getting instead "?".
The content in the html header is set as utf-8.
This is my code
Response.CharSet = "UTF-8"
...
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 2
objStream.CharSet = "utf-8"
objStream.Open
objStream.Position = 0
objStream.LoadFromFile( path )
strData = objStream.ReadText()
Response.write(strData) '<== just to see
objStream.Close
Set objStream = Nothing
At first I was using a single FileSystem object, but read that it has even more problems handling encoding
Internally, VBScript strings are UTF-16 encoded. IO functions that read must be told/assume per default the correct source encoding to convert the source into UTF-16. IO function that write must be told/assume per default the desired output encoding to convert UTF-16 into that desired encoding.
If your file is really (some kind of) ANSI then your
objStream.CharSet = "utf-8"
is wrong. It should be the name of the encoding (cpXXX, ISO_YYY, ZZZ) that your file really uses.
Did you test using the FileSystemObject? Maybe it will guess right and your problem is solved without extra effort.
Related
Inside a more complex script in LUA, I created the following function, that is supposed to
retrieve the list of all audio files located into a directory, which is specified when the function is called (first parameter).
The function returns a formatted HTML line or a CSV one, based on the value of the 2nd parameter.
If 1, it returns HTML
If 2, it returns CSV
This function works fine unless path and/or file names don't have accented letters or umlauts.
Then fails.
here an exaggerated example, with which I've made tests:
J:\PRODUCTION\tüv-ààéérï-utf8\Rendering\tüv-ààéérï-utf8_.wav
I did try to implement solution like the one presented here:
converting UTF-8 string to ASCII in pure LUA
and here:
Handle special characters in lua file path (umlauts)
but unsuccessfully.
Is there a way, in LUA, to get file names and path as they have accented/umlauted characters?
IMPORTANT:
the LUA interpreter is not standalone. It's the one built-in the audio application Reaper.
Hence, I can't install some particular LUA extension apart.
So, I need to write some code but I'm totally lost.
[EDIT]
operating system Windows, but i should work also on Mac and Linux as well
----------------------------------------------
-- SCAN RENDERED AUDIO
----------------------------------------------
function scandir(directory,format)
local i, t, popen = 0, {}, io.popen
t = ''
local f=io.popen('dir '..directory)
for filename in popen('dir "'..directory..'" /b'):lines() do
local extension = filename:match("^.+(%..+)$")
if extension == ".wav" or
extension == ".mp3" or
extension == ".flac" or
extension == ".mov" or
extension == ".ogg" or
extension == ".mp4" then
uriFormat = filename:gsub(" ", "%%20")
if format == 1 then
t = t..'<tr class="Rendered"><td>'..directory..'</td><td>'..tostring(filename)..'</td><td><audio controls src="'..directory..'/'..tostring(uriFormat)..'"/></td></tr>'
elseif format == 2 then
t = t..directory..','..tostring(filename)..','..directory..tostring(filename)..LF
end
i = i + 1
end
end
return t
end
The first problem is converting output of dir command to UTF8 string.
Create file cp.bat somewhere on your disk:
#chcp %1 >nul
Replace
popen('dir "'..
with
popen('C:\\path\\to\\cp.bat 65001 <nul & dir "'..
The second problem is converting input parameter directory (a UTF-8 string passed to the function scandir as argument) to the correct encoding.
io.popen expects its argument to be in 1252 codepage.
So, use function utf8_to_win from this script.
Do not forget to set your codepage (1252) at line #11
This question already has answers here:
Write Chinese chars to a text file using vbscript
(3 answers)
Closed 4 years ago.
Chinese characters cannot be saved in a text file via VBScript.
The VBScript is in a folder whose name is in Chinese: 视窗. The script will create a text file, in which the current working directory will be shown. The Chinese characters cannot be saved in the file. Windows Script Host says "Error: Invalid procedure call or argument". The error will not arise if the folder name is in English.
Path = CreateObject("WScript.Shell").CurrentDirectory
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Path & "\Testing.txt", 8, True)
objFile.WriteLine Path
objFile.close
Is it possible for VBScript to save a file path that contains Chinese characters?
The method openTextFile has another optional parameter - format. Its value is 0 by default which opens the file in ASCII format. To save the chinese characters in the file, you can open the file in UNICODE format by specifying the value of the parameter format = -1. Here is the Reference.
objFso.openTextFile(path,8, true, -1) '-1 = TriStateTrue = Opens the file as Unicode
path = split(wscript.scriptFullName, wscript.scriptname)(0) & "Testing.txt"
set objFso = createObject("scripting.filesystemobject")
set objFile = objFso.openTextFile(path,8, true, -1)
objFile.write path
objFile.Close
set objFile = Nothing
set objFso = Nothing
MoveFile doesn't work if I have russian letters in file path, any way to fix that? If path doesn't have russian letters than all works fine.
Script gives me error - "Path not found"
My VBScript:
Set f = WScript.CreateObject("Scripting.FileSystemObject")
If f.FileExists("C:\Users\AAA\AppData\Local\Temp\tempFile.exe") Then _
f.MoveFile "C:\Users\AAA\AppData\Local\Temp\tempFile.exe", _
"C:\Users\AAA\Desktop\СУППЕР ЗЛО\test.exe"
you will need to save the file in Unicode format instead of UTF-8
I have the following code to read a text file:
Option Explicit
Dim InputFile
Dim FSO, oFile
Dim strData
InputFile = "C:\Program Files (x86)\AVG\CloudCare\ClientVersion.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)
strData = oFile.ReadAll
oFile.Close
msgbox strData
The contents of ClientVersion.txt is:
CLIENT_VERSION_STRING _T("3.5.2") //
When I run the VBS code, I get back this:
If I create a new text file with the same content in the same location, it works fine. Is there a reason why VBS is unable to read this simple text file? I couldn't see any issues with permissions on the file.
ÿþ is the byte order mark of a UTF-16 Little Endian encoded file. UTF-16 (unlike ASCII/ANSI) uses two bytes for a character instead of just one. However, the OpenTextFile method reads files as ASCII files by default, so each 2-byte character gets interpreted as two separate characters.
From the documentation:
Syntax
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
[…]
format
Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as Unicode, TristateFalse = 0 to open the file as ASCII, TristateUseDefault = -2 to open the file as the system default). If omitted, the file is opened as ASCII.
Specify the proper encoding when reading the file and the problem will disappear:
Set oFile = FSO.OpenTextFile(InputFile, 1, False, -1)
I am trying to insert lines into the middle of a non-text file (the extension of the file is "dxf"). I am using vbscript to do this.
Everywhere I have look, I come across the FileSystemObject.OpenTextFile. However, when I try to use this on a dxf file, it is causing an error: Exception 80070057 (I believe this is an invalid file) .
Here is my code:
Dim file
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fileexists(dxfFile$) Then
Set file = fso.OpenTextFile(dxfPath, ForAppending, True)
file.WriteLine("<PORTLIST TESTING>ASDFLKJ")
file.Close
End If
dxfFile$ is not a valid VBscript variable name; use dxfFile, file or dfxPath (consistently)
FileExists is a method of the FileSystemObject; you need to call fso.FileExists
Neither dxfFile, nor dfxPath, nor ForAppending are defined
Calling .OpenTextFile with an undefined/empty first/filespec parameter throws an error 5 - Invalid procedure call or argument
You can't insert lines by appending them; modifying files 'in the middle' is especially clumsy in VBScript; loading the whole file into memory, editing, writing it back may work for you
.DFX file come in ASCII or binary format; if the latter, you can't use the FileSystemObject (see ADODB.Stream)