37

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
  • 473
  • 1
  • 4
  • 4

7 Answers7

51

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

This method will convert to UTF-8-BOM


Further Reading

DavidPostill
  • 162,382
10

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
  • 101
  • 1
  • 2
4

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")
 }
3

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.

1

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
  • 121
1

Although all of the other answers somewhat work, here is a complete batch file you can run from Powershell, where you can supply a directory's path.

  1. In the following script, I am converting *.cpp and *.h files. Change these to whatever files you want to convert.
  2. Save the script into a file with the extension ps1 (e.g., "ConvertToUtf8.ps1")
  3. Execute the script like this: & '.\ConverToUtf8.ps1' 'E:\Source code\Project ABC\Src'

Script:

param([string]$directoryPath)

Get-ChildItem -Path "$directoryPath*" -Include ".cpp",".h" | ForEach-Object { $utf8File = "$($.FullName).utf8" Get-Content $ | Set-Content -Encoding utf8 $utf8File Move-Item -Path $utf8File -Destination $.FullName -Force Echo $.FullName }

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
  • 874