6

OS: Windows 7 SP1

I created an empty text file in the cmd.exe using the below command:

echo 2> .gitignore

The command redirects std::cerr (empty output in this case) into the .gitignore file. The result file has ANSI encoding, but I need UTF-8. Can I point the necessary encoding (UTF-8) for the > operation?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

7 Answers7

5

It's not possible via batch-file output redirection.

The only way to do it with the built-in utilities is to invoke powershell:

powershell -c "[io.file]::WriteAllText('.gitignore','',[System.Text.Encoding]::UTF8)"
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • 1
    I wouldn't say it is the only way, but it certainly can't be done with Windows batch, and PowerShell is a good option. – dbenham Aug 03 '15 at 00:05
  • "using built-in utilities" was on my mind at that moment, added it now. – wOxxOm Aug 03 '15 at 05:05
  • I believe JScript could be used as well, and it is built-in and available to all versions of Windows since XP, without any special privileges or activation required. – dbenham Aug 03 '15 at 11:43
  • 1
    Ah, indeed, strictly speaking you're right, however a [jscript solution](http://stackoverflow.com/questions/2840252/writing-utf8-text-to-file) isn't what I would like to endorse or even remember... – wOxxOm Aug 03 '15 at 11:48
  • @dbenham it's possible with native batch, please see my answer – phuclv Jan 11 '18 at 13:08
3

Pure batch solution, based on Generate nearly any character, including TAB, from batch by dbenham

@echo off

(set LF=^
%=empty=%
)

::Create variables to store BOM bytes
call :hexprint "0xEF" EF
call :hexprint "0xBB" BB
call :hexprint "0xBF" BF

<nul SET /P "=%EF%%BB%%BF%"> output.txt
exit /b

:hexPrint  string  [rtnVar]
  for /f eol^=^%LF%%LF%^ delims^= %%A in (
    'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"'
  ) do if "%~2" neq "" (set %~2=%%A) else echo(%%A
exit /b
phuclv
  • 37,963
  • 15
  • 156
  • 475
1

Create a .bat/.cmd file like:

<nul SET /P "=123"> output.txt

Then replace 123 with EF BB BF bytes in your prefered HEX-editor.

To edit this .bat/.cmd file later one shouldn't use Window's Notepad.exe as it converts BOM-bytes to a question mark (?) in the "Save as ASCII" mode (or, in the "Save as UTF-8" mode it adds unneeded BOM to the script file itself). Instead one can use Notepad++ with a "UTF-8 (witout BOM)" mode.

oshatrk
  • 499
  • 3
  • 14
  • 1
    Why is this downvoted? If this works correctly, I don't see the problem. If you're messing around with batch files you'll probably be able to hex edit a file with not much problem. – OMA Jan 24 '21 at 02:45
1

Hybrid batch - JScript solution. Just save as a batch file and run normally

@if (@CodeSection == @Batch) @then

@echo off
cscript //e:jscript //nologo "%~f0" %1
exit /b

@end

// JScript Section

var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(WScript.Arguments.Item(0), true);

file.Write(String.fromCharCode(239));
file.Write(String.fromCharCode(187));
file.Write(String.fromCharCode(191));
file.Close();

Same as above, but hybrid batch - VBS

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf" %1
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
    Set fso  = CreateObject("Scripting.FileSystemObject")
    Set file = fso.CreateTextFile(WScript.Arguments.Item(0), true)

    file.Write Chr(239)
    file.Write Chr(187)
    file.Write Chr(191)
    file.Close
</script></job>
phuclv
  • 37,963
  • 15
  • 156
  • 475
1

Pure batch to create an empty UTF-8 file with BOM:

set _FILE=output.txt
chcp 437 > nul
forfiles /c "cmd /c <nul set /p=0xEF0xBB0xBF>\"%_FILE%\"" > nul
chcp 65001 > nul

and to add lines:

>> "%_FILE%" (
    echo Line 1
    echo Line 2
)
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

I have an empty text file with the BOM, which I copy, and then append the stuff I need in this file.

copy empty-bom.txt .gitignore
echo stuff>>.gitignore

I prefer this solution because it is so much more readable and understandable than all the other solutions that generate the BOM.

htho
  • 1,549
  • 1
  • 12
  • 33
-1

This will give you utf 16, start cmd with /u switch

cmd /u /c type ansi.txt > uni.txt

/u make internal commands output UTF16.

bill
  • 215
  • 1
  • 3