I'm trying to write some Chinese characters to a text file using
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set outputFile = myFSO.OpenTextFile(getOutputName(Argument, getMsiFileName(Wscript.Arguments)), forWriting, True)
outputFile.WriteLine(s)
The variable s contains a Chinese character that I read from the other file. I echo s value and I can see the s correctly in the screen. However, for some reason the script stops running after outputFile.WriteLine(s) without returning any error message.
Am I missing something?
Maybe it's got something to do with character encoding. Try directly specifying the Unicode format for the file in the last parameter of the OpenTextFile method:
Const Unicode = -1
Set outputFile = myFSO.OpenTextFile(getOutputName(Argument, getMsiFileName(Wscript.Arguments)), forWriting, True, Unicode)
Also, you need to close the file after writing to it:
outputFile.Close
If this doesn't help, try error handling like AnthonyWJones suggested.
Try this:-
MsgBox "Writing Line"
On Error Resume Next
outputFile.WriteLine s '' # Removed ( ) that shouldn't be there.
MsgBox "Err " & Err.Number & ": " & Err.Description
On Error GoTo 0
What do you get?
Things had changed along win versions. This works on Win10:
Set outputfile = myFSO.CreateTextFile(filename,True,True)
The 3rd arg is bool true/false for unicode/ascii 2. Surprisingly, when using myFSO.OpenTextFile the arg is int with 1 (not -1) for unicode 1.
Documentation:
https://msdn.microsoft.com/en-us/library/aa265018(v=vs.60).aspx
https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
Related
I've been writing code to process xml downloaded via a webservice API. I was going ok until one query had some mysterious characters before the root element.
After contacting the support, I got the following message...
"The ABS.Stat APIs resultant XML output are UTF-8 compliant. These characters are a UTF-8 Byte Order Mark designed to identify the xml as UTF-8. Hope this helps."
Whilst waiting for their reply I continued with my programming by simply starting my DOM processing at the opening tag (first "<") with the following code...
Dim lgRootElementStart As Long
lgRootElementStart = InStr(1, hReq.ResponseText, "<")
Dim sgResponse As String
sgResponse = Mid(hReq.ResponseText, lgRootElementStart)
Dim xmlDoc As New MSXML2.DOMDocument
If Not xmlDoc.LoadXML(sgResponse) Then
etc. etc. etc.
All seems to be well, the data is deciphered and displayed ok.
But now that I know what those characters are, is there anything I should do with those characters?
Or to put it another way, is there anything I can do with those characters to make my excel application more reliable? i.e. now that I know the XML is UTF-8, how should I process it differently?
What should I do if the BOM gives UTF-16?
Well it seems that the BOM is more a nuisance than helpful, but I placed code in my application to check that it is a UTF8 BOM if any characters before the xml root element are received. If it's not a UTF8 BOM then an error is thrown. I'm not expecting this to be a problem any more, but if I ever see the error then I will have to re-analyse what's going on. Hopefully that will never happen.
Code is...
Public Const BOM_UTF8 As String = ""
and
If lgRootElementStart > 1 Then
If Left(hReq.ResponseText, lgRootElementStart - 1) = BOM_UTF8 Then
Else
Err.Raise ERROR_SHOULD_NEVER_HAPPEN, sFunctionName, _
"Non UTF8 BOM found. " _
& "BOM is ..." & ConvertToHex(Left(hReq.ResponseText, lgRootElementStart - 1)) _
& ", correct BOM is ... " & ConvertToHex(BOM_UTF8)
End If
End If
One quote from a link in the comments says..."Encodings should be known, not divined". Well with this code I know it's UTF8 if I get it.
I have encountered something really weird. When exporting to CSV my top line shows the quotation marks yet the lines below down.
I use UTF8 encoding and manually add the double quotation marks to the value so that it is encased with quotation marks.
the code being used is
Dim fs As New IO.FileStream(GenericValueEditorExportFilename.Value, IO.FileMode.Create)
Dim writer As New IO.StreamWriter(fs, Encoding.UTF8)
fs.Write(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length)
....
....
....
While reader.Read
If reader("TargetLanguageID") = targetLanguageID Then
writer.WriteLine(Encode(reader("SourcePhrase")) & ", " & Encode(reader("TargetPhrase")))
End If
....
....
....
Friend Shared Function Encode(ByVal value As String) As String
Return ControlChars.Quote & value.Replace("""", """""") & ControlChars.Quote
End Function
the result when displayed in excel is shown as (https://ibb.co/ntMYdw)
when i open the file in Notepad++ the text is shown as below. But each line is displayed differently. Why is it that the 1st row displays them and the 2nd does not. Notepad++ result is displayed as (https://ibb.co/fMkWWG)
Excel is treating the first line as headers.
https://stackoverflow.com/a/24923167/2319909
So the issue was being caused by the BOM that was created to manually set the encoding for the file as a start writing to the file.
fs.Write(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length)
Removing this resolves by issue and the file remains in the desired UTF8 encoding as it is set on the stream writer. so there is no need to add the BOM to set the encoding.
Something like this should work for you.
Dim str As New StringBuilder
For Each dr As DataRow In Me.NorthwindDataSet.Customers
For Each field As Object In dr.ItemArray
str.Append(field.ToString & ",")
Next
str.Replace(",", vbNewLine, str.Length - 1, 1)
Next
Try
My.Computer.FileSystem.WriteAllText("C:\temp\testcsv.csv", str.ToString, False)
Catch ex As Exception
MessageBox.Show("Write Error")
End Try
I am using secureCRT to connect to a Linux server. SecureCRT reads VBScript, and I am new to this language so my problem might sound easy for you.
When I connect to the server from secureCRT, I a script containing a command let's say "date" ,the output of the command must come out on a text file on my local host ( windows ) and not on the server.
This is the script that I am using:
# $language = "VBScript"
# $interface = "1.0"
' This script demonstrates how to capture line by line output
' from a command sent to a server. It then saves each line of output
' to a file. This script shows how the 'WaitForStrings' command can be
' used to wait for multiple possible outputs.
' Constants used by OpenTextFile()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Sub Main
crt.Screen.Synchronous = True
' Create an instance of the scripting filesystem runtime so we can
' manipulate files.
'
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
' Open a file for writing. The last True parameter causes the file
' to be created if it doesn't exist.
'
Set file = fso.OpenTextFile("C:\Users\elieme\Desktop\TTX1.txt", ForWriting, True)
' Send the initial command then throw out the first linefeed that we
' see by waiting for it.
'
crt.Screen.Send "gsh list_imsins" & Chr(10)
crt.Screen.WaitForString Chr(10)
' Create an array of strings to wait for.
'
Dim waitStrs
waitStrs = Array( Chr(10), "linux$" )
Dim row, screenrow, readline, items
row = 1
Do
While True
' Wait for the linefeed at the end of each line, or the shell prompt
' that indicates we're done.
'
result = crt.Screen.WaitForStrings( waitStrs )
' If we saw the prompt, we're done.
If result = 2 Then
Exit Do
End If
' The result was 1 (we got a linefeed, indicating that we received
' another line of of output). Fetch current row number of the
' cursor and read the first 20 characters from the screen on that row.
'
' This shows how the 'Get' function can be used to read line-oriented
' output from a command, Subtract 1 from the currentRow to since the
' linefeed moved currentRow down by one.
'
screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 45 )
' NOTE: We read 20 characters from the screen 'readline' may contain
' trailing whitespace if the data was less than 20 characters wide.
' Write the line out with an appended '\r\n'
file.Write readline & vbCrLf
Wend
Loop
crt.screen.synchronous = false
End Sub
I read the script several time to understand it, and tried to manipulate it for hours, and asking for help was my last resort.
In the script I have crt.Screen.Send "date" & Chr(10) which will send my command and execute it. Then when I go in the loop, I don't understand what does this mean.
'If we saw the prompt, we're done.
If result = 2 Then
Exit Do
End If
What do they mean by if we saw the prompt? Is it something that i have to input for it to exit ? Because i tried several keys and I'm always stuck in this while loop.
I tried to manipulate the script but every time I was either getting an empty file as output, or wrong data in the file.
Is there anyway to make this script execute only the command that I sent ("date"), and output what this command will do on the text file ?
If not, is there any shortcut to stop the script without having to go in the menu and selecting cancel script?
Thank you
EDIT:
I fixed it guys, very easy.
The rt.Screen.WaitForStrings takes as a second parameter a timeout number, so that fixed everything.
Thanks
Fixed
The rt.Screen.WaitForStrings takes as a second parameter a timeout number, so that fixed everything.
I have a script that lists all files in a directory, then for each one it will Response.Write the name and how many downloads it has.
I have everything completed, but when I went for a test, the files that have "odd" characters in the name are replace with a ?
I'm guessing, that since some files have foreign languages as there name, and that some have the iPhone emoji icons in the name, that it doesn't recognize it and puts a ? instead, but this is a serious issue since I can't give the correct file name back to the user, then that incorrect name is fed back into the url to download. (Which doesn't work)
Any suggestions?
Edit:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("."))
for each file in fo.files
if fs.GetExtensionName(file.Path) = "plist" then
dim tempList, tempName, ...
tempList = split(file.Name, ".")
'Manipulate name and data ...
Response.write(name)
end if
next
The file names themselves have odd characters, and file.Name returns a ? instead of what is actually there.
18アイコン is one example.
Here's some code which works fine for me:
<%# Language="VBScript" CodePage="65001" %><%
Option Explicit
Response.CodePage = 65001
Response.CharSet = "utf-8"
Dim fs, fo, file
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(Server.MapPath("."))
For Each file In fo.files
If fs.GetExtensionName(file.Path) = "plist" Then
' Do whatever here...
Response.Write file.Name & "<br>"
End If
Next
%>
If you are using any variables that you didn't dimension beforehand, you'll need to remove the Option Explicit; otherwise, VBScript will complain that you didn't dimension them.
Edit: I copy & pasted the wrong code; this code works.
I am fairly new to VBA and am stumped on how to resolve the "Run-time error '5': Invalid procedure call or argument" error that I am receiving when executing this code. The cell in question has chinese characters and the code seems to work fine on the english alphabet. The stream is outputting to a text file. (should be an xml file in the future, but I still don't have all the correct formatting implemented)
Dim fso As New FileSystemObject, stream As TextStream
Set stream = fso.createTextFile("C:\Users\username\XMLs\" _
& WS_Src.Cells(c.Row, 5).Value & "_" & WS_Src.Cells(c.Row, 4).Value & "_Feature.xml", True)
...
stream.WriteLine "<title>" & vbCrLf & "<![CDATA[ " & WS_Src.Cells(c.Row, 6).Value & "]]>" & vbCrLf & "</title>" 'error is on this line
stream.Close
Thanks!
Chris
Sytax for using CreateTextFile method is something like object.CreateTextFile(filename[, overwrite[, unicode]])
. Where:
filename: Required. String expression that identifies the file to create.
overwrite Optional. Boolean value that indicates if an existing file can be overwritten. The value is True if the file can be overwritten; False if it can't be overwritten. If omitted, existing files are not overwritten.
unicode Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is True if the file is created as a Unicode file; False if it's created as an ASCII file. If omitted, an ASCII file is assumed.
And you have omitted the last param here, but incoming text, being Chinese is not just ASCII. Rather you have to provide a True value for that, I mean for unicode param. This would definitely solve the problem.
BTW! There are still some factors I can see in the code might cause other run-time errors.
As you generating filename by joining cell values, make sure no invalid characters is not present in the path string,
Furthermore, only setting overwrite value to true is not enough, but also make sure that the folder already exist. Otherwise the procedure would again caught by run-time errors.
Hope this helps.