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

Share:
5,213

Related videos on Youtube

Neil McGuigan
Author by

Neil McGuigan

Updated on September 18, 2022

Comments

  • Neil McGuigan
    Neil McGuigan about 1 year

    Something like:

    echo \ufeff > output.csv
    type input.csv >> output.csv
    

    Where \ufeff is the UTF-8 BOM, and input.csv is a UTF-8 text file.

    How do I indicate a UTF-8 character in cmd?

    • ott--
      ott-- over 8 years
      See if stackoverflow.com/questions/388490/… helps you. And later, when you have the character in file1, do copy file1+file2 newfile.
    • Neil McGuigan
      Neil McGuigan over 8 years
      Thanks. How do I actually indicate \ufeff in the command line though?
    • ott--
      ott-- over 8 years
      Maybe it's easier to use Notepad++ for this. Go to Encoding and set Encode in UTF-8, that will set a BOM.
    • Neil McGuigan
      Neil McGuigan over 8 years
      @ott I'm trying to do this on the command line :)
    • ott--
      ott-- over 8 years
      To create a template with just a BOM in it.
    • Neil McGuigan
      Neil McGuigan over 8 years
      k 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?