Write an empty file using the UTF8 specification only

OS Windows 7 SP1
I create an empty text file in cmd.exe :

 echo 2> .gitignore 

This command redirects std::cerr (empty output in this case) to a .gitignore file. The result file has ANSI encoding, but I need utf-8 . Can I specify the necessary encoding ( utf-8 ) additionally for the > operation?

+4
source share
4 answers

This is not possible by redirecting the package output files.

The only way to do this with the built-in utilities is to call powershell:

 powershell -c "[io.file]::WriteAllText('.gitignore','',[System.Text.Encoding]::UTF8)" 
+4
source

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

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

/u do internal UTF16 output commands.

0
source

Create a .bat / .cmd file, for example:

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

Then replace 123 bytes of EF BB BF in your preferred HEX editor.

To edit this .bat / .cmd file later, you should not use Window Notepad.exe, because it converts BOM bytes to a question mark ( ? ) In the "Save as ASCII" mode (or, in the "Save as UTF-8" mode adds an unnecessary specification to the script file). Instead, you can use Notepad ++ with the "UTF-8 (witout BOM)" mode.

0
source

Pure batch solution based on Generates almost 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 
0
source

All Articles