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?
To create an empty file on the Windows Command line, you can utilize Command Prompt or PowerShell. On Command Prompt, many commands are available to create a new file or empty file, such as “cd.”, “copy nul”, ”break”, “echo.”, “type nul”, and “call”.
The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use. Byte order has no meaning in UTF-8, so its only use in UTF-8 is to signal at the start that the text stream is encoded in UTF-8, or that it was converted to UTF-8 from a stream that contained an optional BOM.
The UTF-8 file signature (commonly also called a "BOM") identifies the encoding format rather than the byte order of the document. UTF-8 is a linear sequence of bytes and not sequence of 2-byte or 4-byte units where the byte order is important.
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With