Converting text file to UTF-8 on Windows command prompt

15

6

I need to convert a text file to UTF-8 format via Windows command prompt. This needs to be done on another machine and I do not have rights to install software on that machine. I need something like:

c:\notepad   source-file target-file --encoding option

Is there a Windows command prompt utility which can do it?

user1107888

Posted 2017-01-05T13:58:22.553

Reputation: 253

Answers

28

I need to convert a text file to utf-8 format via windows command prompt

You can easily do this with PowerShell:

Get-Content .\test.txt | Set-Content -Encoding utf8 test-utf8.txt

Further Reading

DavidPostill

Posted 2017-01-05T13:58:22.553

Reputation: 118 938

Additionally, you need to switch to powershell in order to execute the command and then exit it to go back to windows command prompt. – user1107888 – 2017-01-05T16:09:50.937

Indeed. Or run it directly in a PowerShell shell. – DavidPostill – 2017-01-05T16:44:27.190

@DavidPostill any idea how we can convert more than one file i.e batch file processing with the same command line ? – vibs2006 – 2019-01-28T09:49:19.390

1

@vibs2006 Use a ForEach loop. See ForEach - PowerShell - SS64.com.

– DavidPostill – 2019-01-28T18:32:16.137

Will this method convert to plain UTF-8 or UTF-8-BOM? – Cale Sweeney – 2019-10-23T22:28:08.403

1

Use iconv from GNUWin32 pack. It is much faster, especially if your files are about or more than 1 Gb.

"C:\Program Files (x86)\GnuWin32\bin\iconv.exe" -f cp1251 -t utf-8 source.txt > result.txt

Raul N-k

Posted 2017-01-05T13:58:22.553

Reputation: 11

what does the cp1251 flag do ? – lacostenycoder – 2019-03-11T16:50:28.057

it's the input encoding. https://en.wikipedia.org/wiki/Windows-1251

– nadre – 2019-05-17T08:57:48.410

1

Here is for each convert *.text file to *.sql file:

foreach ($file in get-ChildItem *.txt) {
    Echo $file.name
    Get-Content $file | Set-Content -Encoding utf8 ("$file.name" +".sql")
 }

nobjta_9x_tq

Posted 2017-01-05T13:58:22.553

Reputation: 111

after run some sql file, sometime error, I decided to using linux with iconv command =)) – nobjta_9x_tq – 2019-05-28T15:46:01.140