What is a simple way to prepend a UTF-8 character to a text file using cmd?

0

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?

Neil McGuigan

Posted 2015-04-22T18:00:31.247

Reputation: 339

See if http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how/388500#388500 helps you. And later, when you have the character in file1, do copy file1+file2 newfile.

– ott-- – 2015-04-22T18:20:34.483

Thanks. How do I actually indicate \ufeff in the command line though? – Neil McGuigan – 2015-04-22T18:25:14.053

1

Another link http://stackoverflow.com/questions/11962172/echo-utf-8-characters-in-windows-batch but it uses not the standard echo. Do you want to insert a BOM into a file?

– ott-- – 2015-04-22T18:35:11.857

Maybe it's easier to use Notepad++ for this. Go to Encoding and set Encode in UTF-8, that will set a BOM. – ott-- – 2015-04-22T19:40:23.987

@ott I'm trying to do this on the command line :) – Neil McGuigan – 2015-04-22T19:41:08.970

To create a template with just a BOM in it. – ott-- – 2015-04-22T19:59:49.003

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? – Neil McGuigan – 2015-04-22T20:33:53.127

Answers

1

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

Neil McGuigan

Posted 2015-04-22T18:00:31.247

Reputation: 339