What encoding to get Å Ä Ö to work

12

1

I'm writing a small application in VB.net that will enable me to easy create a user "Windows 7 account" with a password, instead of going though the control panel. The problem I'm having is that when I create a bat file in VB.net using UTF-8 encoding it's doesn't write å ä ö as it's suppose to be. I have tried all encodings I can find but are unable to get it working.

If anyone got an idea on why I'm getting this please let me know. Thanx in advance!

kagstrom2100

Posted 2013-11-13T11:20:44.753

Reputation: 143

try posting on stackoverflow.com and show your script. – Toby Allen – 2013-11-13T11:34:20.297

1@Toby Allen Well it's not really a programming question but a question about encoding text, the same problem occurs when I create a .bat file the "normal" way. – kagstrom2100 – 2013-11-13T11:37:12.890

Answers

12

Edit: I was wrong ;)
cmd.exe does accept UTF-8 but you need to be sure to save it without the BOM at the beginning of the file.

Here is a second test. You can use chcp 65001 at the beginning of your batch-file.

enter image description here


A batch file can not be of type UTF-8. It needs to be ASCII. Cmd.exe just doesn't accept another format. I did a small test and you can use your characters but it needs some work.

Make a file test.bat with echo Å Ä Ö. Save it with format ANSI/ASCII. Open a cmd.exe and make sure your cmd.exe uses Lucida Console (to display the Unicode characters).

When you type the file it will show characters as the old DOS-characters. You can see a translation chart here.

When you switch to a "Windows Ansi"-code page (i.e. West European Latin) with chcp 1252 the characters are displayed correctly. If they also get transferred to their respective utilities depends on that utility.

But why are you creating a batch-file for this? Can't you just code it in VB.net?

cmd.exe


Edit 2#:

This is how you set Lucida Console in cmd.exe:

Lucida Console


The BOM are 3 characters at the beginning of a UTF-8 file. (\xEF\xBB\xBF).
In VB.net you would create a file without a BOM like this:

Dim utf8WithoutBom As New System.Text.UTF8Encoding(False)
                                                  '^^^^^'
Using sink As New StreamWriter("Foobar.txt", False, utf8WithoutBom)
    sink.WriteLine("...")
End Using

Rik

Posted 2013-11-13T11:20:44.753

Reputation: 11 800

NB: doing chcp 65001 followed by ping 127.0.0.1 -n 2 | find " = " will cause your computer to hang; I think the character set affects find (as the ping without this filter works perfectly) – JohnLBevan – 2015-04-16T16:34:28.307

2@JohnLBevan Yeah, you're right. Apparently find doesn't handle UTF-8 input not too well. But you can use findstr. Like this: ping 127.0.0.1 -n 2 | findstr /C:" = " – Rik – 2015-04-16T19:01:41.210

Hi and thanx for the answer. In my vb.net application I'm trying to create a .bat file with the commands: net user "username" net localgroup administratörer "type" /add The reason i have to use the Swedish word adminstratörer instead of administator is beause localgroup won't take the English translation as a group. – kagstrom2100 – 2013-11-14T09:10:58.030

Just thought going through a .bat-file was a bit much when you could do this straight from VB.net. Like here. Lots of examples in Google too. So no need to create a .bat as middle-man. And with those examples you could feed the Unicode-name directly to the Windows-function. (Unless you need to run the .bat at a different time separate from your VB.net app for some reason?)

– Rik – 2013-11-14T09:31:30.870

Well I have been looking for a way to do this in VB.net on Google for days, though I haven't found any way do do it. I already tried the example you linked and it didn't seem to work.

How do I make sure that the .bat file doesn't use BOM? Also what is Lucida Console? The only info I could find about it is that it's a font.

When I try the exact thing you did in the second example/image I get this http://i.imgur.com/6sh20j0.jpg

– kagstrom2100 – 2013-11-14T09:36:06.273

I added to my answer to include Lucida Console and file-creation without BOM (Edit #2, at the bottom). But does creating this user via net user work on the command-prompt? And what does not work if you create it via VB.net? For this we would need some example-code (or a separate question) because this should work. (BTW you linked my image, not yours, i think, in your comment) – Rik – 2013-11-14T10:41:31.213

So I changed it to Lucia Console and not it seems to work. Though I need the script to run on other machines too and the Lucia console is only saved on the machine i changed it on right? I made a separate question for how to do this in VB.NET instead here http://stackoverflow.com/questions/19974473/making-a-windows-user-using-vb-net P.S Yes creating a user in command-prompt does work and yes I accidentally linked the wrong image :P

– kagstrom2100 – 2013-11-14T11:11:32.363

Actually for the batch file the font wouldn't be that important (it's only for display on screen). The .bat should just run fine on the other machines without setting the cmd.exe to Lucida Console. (You would need the chcp 65001 though). Does your .bat do the correct job now or do you still have troubles with it? – Rik – 2013-11-14T11:31:17.357

Yeah, I know that the font doesn't affect if the command works or not. Yes as far as I can tell it's working right now. I'll implement it into my application, I'll post again if I get any more problems! Thanx man :D – kagstrom2100 – 2013-11-14T11:47:25.383

Tested some more now by creating a .bat file using VB.NET Here is the code My.Computer.FileSystem.WriteAllText(CurDir() + "\adduser.bat", "chcp 65001" + Environment.NewLine + "net user " + UserBox.Text + " " + PaswordBox.Text + "/add" + Environment.NewLine + "net localgroup " + UserType + " " + UserBox.Text + " " + "/add " + Environment.NewLine + "pause", False, utf8WithoutBom)

Though the CMD window opens for a split second, which would indicate that it cannot read the text. – kagstrom2100 – 2013-11-14T12:12:21.890

Do a type adduser.bat on the cmd.exe. What do you get? Are there unreadable characters on the first line? And you didn't use StreamWriter but My.Computer.FileSystem.WriteAllText. Maybe My.Computer.FileSystem.WriteAllText doesn't do the non-BOM thing. so rewrite it to use StreamWriter like in my example. – Rik – 2013-11-14T12:19:22.303

You could also try My.Computer.FileSystem.WriteAllText(CurDir() + "\adduser.bat", Text, False, Encoding.ASCII). See here. Not sure if it keeps the utf-8 characters but you can try ;)

– Rik – 2013-11-14T12:27:32.847

let us continue this discussion in chat

– kagstrom2100 – 2013-11-14T12:47:01.030

2

The thing that fixed this for me was to save the file as UTF-8 without BOM and using this code

@echo off
chcp 65001
net user Linus /add
net localgroup Administratörer Test/add

The thing I didn't use before was @echo off so that and using chcp 65001 is what fixed it! Thanx too Rik for all the help :)

kagstrom2100

Posted 2013-11-13T11:20:44.753

Reputation: 143