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?

user3575900
  • 31
  • 2
  • 5

1 Answers1

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 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. – user3575900 Aug 10 '14 at 23:51
  • 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. – user3575900 Aug 11 '14 at 00:04
  • `` is the [byte order mark](http://en.wikipedia.org/wiki/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](http://stackoverflow.com/a/15230319/1630171) before appending it to the output file. – Ansgar Wiechers Aug 11 '14 at 13:45
  • 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) – user3575900 Aug 11 '14 at 17:44
  • "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? – user3575900 Aug 11 '14 at 17:46
  • I actually need to to prepend the data in the String StrVar to the temp file and rename it to csv. that's all that needs to be done. Because the temp file is too large, I wish not to append the temp file to the string in another file but instead use the copy command in cmd to concatinate the files and append it to a csv. – user3575900 Aug 11 '14 at 18:10
  • @user3575900: Forget about VBScript and `copy`. VBScript is bad at handling UTF-8, and the `copy` command isn't aware of the encoding (which is why the BOM of each source file is preserved in the destination file). Use PowerShell instead: `'text to append' | Add-Content 'C:\path\to\your.csv' -Encoding UTF8`. – Ansgar Wiechers Aug 12 '14 at 10:28
  • doing it for client. That's why had to use cmd on windows xp.I used the adodb stream as you suggested, and it worked well. However had to read the entire temp file line by line, though it doesn't take up as much time as I had expected. Thanks a ton for the help. – user3575900 Aug 13 '14 at 15:54