What is a simple way to prepend a UTF-8 character to a text file using cmd?
5,213
Thanks to @ott for the tips. Here's how to do it:
Use Notepad++ with the TextFX plugin
Create a batch file addbom.bat
in Notepad++, then Main Menu > Encoding > Encode in UTF-8 without BOM
Enter the following:
@echo off
set "BOM=EF BB BF"
<nul set /p = %BOM%> output.csv
type %1 >> output.csv
Hightlight EF BB BF
then Main Menu > TextFX > TextFX Convert > Convert Hex to Text. This changes the text to:
@echo off
set "BOM="
<nul set /p = %BOM%> output.csv
type %1 >> output.csv
Save the file, then in the command prompt run addbom bad.csv
. Your BOM'd csv will be in output.csv
<nul set /p=
is the same as echo
but doesn't produce a newline
Related videos on Youtube
Author by
Neil McGuigan
Updated on September 18, 2022Comments
-
Neil McGuigan about 1 year
Something like:
echo \ufeff > output.csv type input.csv >> output.csv
Where
\ufeff
is the UTF-8 BOM, andinput.csv
is a UTF-8 text file.How do I indicate a UTF-8 character in cmd?
-
ott-- over 8 yearsSee if stackoverflow.com/questions/388490/… helps you. And later, when you have the character in
file1
, docopy file1+file2 newfile
. -
Neil McGuigan over 8 yearsThanks. How do I actually indicate \ufeff in the command line though?
-
ott-- over 8 yearsMaybe it's easier to use
Notepad++
for this. Go toEncoding
and setEncode in UTF-8
, that will set a BOM. -
Neil McGuigan over 8 years@ott I'm trying to do this on the command line :)
-
ott-- over 8 yearsTo create a template with just a BOM in it.
-
Neil McGuigan over 8 yearsk i created bom.csv as an empty file with UTF-8 encoding (w bom), then append the real csv to it and it works. Can it be done without the extra file though?
-