Write an empty file with just the UTF-8 BOM
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)"
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 bytescall :hexprint "0xEF" EFcall :hexprint "0xBB" BBcall :hexprint "0xBF" BF<nul SET /P "=%EF%%BB%%BF%"> output.txtexit /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(%%Aexit /b
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.