1

I have written the following code segment:

strVar = "one line that I want to write in the text file with extension .csv" 'Set fso = CreateObject("Scripting.FileSystemObject") 'fso.CreateTextFile(str2) 'Set f = fso.OpenTextFile(str2,2,True) 'f.WriteLine(str4) 'str2 holds the name of the file, say abc.csv //this is a comment 

the default encoding for text files, I think, is ANSI. However, I want to encode it to IUTF-8. the reason I want to do so is that I have a .temp file encoded in UTF-8 format to append to this csv file, and that is how I need the encoding to be post appending. the temp file is a csv file only, just that it has .temp extension. If I just create the textfile as above, after appending there are a couple of errors in the document.

i am using the following command to append the temp file to the csv file via command prompt which is called by the vbscript:

"copy " &str2 & "+" &str1 & " " &str2 

this produces the errors. str1 is the temp file name.

If I can create the text file as with UTF-8 encoding, it would work fine. Alternatively, after I append the temp file to the csv, if I can convert the csv as to have the properties of the temp files, including the encoding, it should be fine too.

Any ideas how any one of these could be done?

1 Answer 1

1

If you want to append to a file, simply open the file for appending:

Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("C:\path\to\your.csv", 8, True) f.WriteLine "text to append" f.Close 

You also don't need to use CreateTextFile() first. The 3rd parameter of OpenTextFile() already takes care of that.

As far as printable characters are concerned, there is no difference between ISO-8859-1 and what Microsoft calls "ANSI" in textfiles.

Sign up to request clarification or add additional context in comments.

8 Comments

The error is produced when I try to append the temp file to the csv. The temp file is very big in size, so I avoided readline which is time consuming and readall which is memory demanding. Your code would work just as fine as the first segment of mine, but that's not the issue.
I see characters like () at the point where the two files are appended as well as at the end of file. If there were actually no differences, those unreadable characters should have not appeared.
 is the byte order mark for UTF-8 encoded files. Check where the string you're trying to append came from. You may need to convert it before appending it to the output file.
I think that is also the case for the character set ISO-8859-1. Maybe I am wrong. Anyways, I should have mentioned UTF-8 as that is the encoding in my case. the string that I want to append is in a variable. I tried to convert it the way your link shows, but the errors still exist the same way as they did before. i understand that your code to encode the string is perfectly fine, but I have errors. Here' what I am doing in cmd after encoding the string strVar: (continued)
"copy " &strVariable & "+" &str1 & " " &str2 where strVariable is the string i have encoded the way you described, str1 is the temp file and str2 is the csv file that has not yet been created. Do you think this could be due to the default ANSI property of the csv file?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.