views:

542

answers:

1

I have the following windows batch command as part of a larger batch file:

type *.sql > dbScript.txt
ren dbScript.txt dbScript.sql

As you can see it joins all SQL scripts in a particular directory into one so that it can be executed during the database build process.

My problem is that I now have non ANSI characters in one of the SQL script files but dbScript.txt (and subsequently dbScript.sql) are being encoded as ANSI.

How can I change the encoding of the output file to UTF-8?

A: 

You'd have to run a program like iconv to convert your output file. cmd only supports either the Windows legacy codepage (by default) or UTF-16 (with the /u argument to cmd) as output encodings.

Alternatively, if the encoding problems are introduced because you are typeing the files and redirecting the output, you can also try something like the following, which should retain encodings (as it doesn't output and then write the text again):

setlocal enableextensions enabledelayedexpansion
set LIST=NUL
for %%f in (*.sql) do set LIST=!LIST!+%%f
copy %LIST% dbScript.sql
endlocal

This would first generate a plus-separated list of SQL files and then use copy to concatenate them.

Joey
shouldnt that read `copy !LIST! dbScript.sql`
akf
Doesn't need to. In that case we're at a point where immediate expansion doesn't hurt. But yes, it would work as well. It's only necessary that the `set` command in the `for` loop uses !LIST! because we need delayed expansion there.
Joey