31

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?

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
user1107888
  • 413
  • 1
  • 4
  • 4

6 Answers6

48

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
  • 153,128
  • 77
  • 353
  • 394
7

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
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
Raul N-k
  • 71
  • 1
  • 1
3

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
  • 131
  • 2
1

You can do this from the command prompt as follows:

powershell -command "Get-Content .\test.txt" > test-utf8.txt

It turns out that piping the output to a file from the command prompt saves as utf-8.

0

POWERSHELL: # Assumes Windows PowerShell, use -Encoding utf8BOM with PowerShell Core. For multiple files:

FIRST SOLUTION:

$files = Get-ChildItem c:\Folder1\ -Filter *.txt 

foreach ($file in $files) {

    Get-Content $file.FullName | Set-Content "E:\Temp\Destination\$($file.Name)" -Encoding utf8BOM

}

OR, SECOND SOLUTION (for multiple files):

get-item C:\Folder1*.* | foreach-object {get-content -Encoding utf8BOM $_ | out-file ("C:\Folder1" + $_.Name) -encoding default}

OR, THE THIRD SOLUTION: (only for 2 files)

$a = "C:/Folder1/TEST_ro.txt"
 $b = "C:/Folder1/TEST_ro-2.txt"
 (Get-Content -path $a) | Set-Content -Encoding UTF8BOM -Path $b
Just Me
  • 816
  • 1
  • 16
  • 38
0

For those who want to batch convert several files (e.g.: all *.txt files in folder and sub-folders):

dir *.txt -Recurse | foreach {
  # May remove the line below if you are confident
  Copy-Item $_ $_.bkp
  
  # Note that since we are reading and saving to the same file,
  # we need to enclose the command in parenthesis so it fully executes 
  # (reading all content and closing the file) before proceeding
  (Get-Content $_) | Set-Content -Encoding utf8 $_
}
J.Hudler
  • 111
  • 4