I have a batch script which makes a call with sqlcmd to pull the results of a SELECT statement into a file called temp.txt. There are some foreign characters in the data that require us to use Unicode, so temp.txt is Unicode (codepage 65001).
Once the data is in temp.txt, the script counts the number of rows and appends some headers. In order to do this, it must create a new file (let’s call it newfile.txt), add the headers and row count, and then copy in each line from temp.txt to newfile.txt.
All of this works fine, except that the first line copied in from temp.txt has a Unicode byte order mark in it; this means that the first line, instead of looking like this:
1, Custom Page
looks like this instead:
1, Custom Page
I cannot figure this out the best way to handle this.
If I could tell sqlcmd to give me Unicode without a BOM, that would be perfect–tried googling around, couldn’t figure it out.
If I could figure out how to write a batch file FOR loop that removes the first three characters of only the first line when copying in temp.txt, I’d try that, but after some googling and experimentation I’m frustrated there.
For the record, the relevant code looks like this:
::%1 = sql file to call; %2 = filename to be created; %3 = header for file; %4 = data type row for file
sqlcmd -I -f 65001 -W -k 1 -h -1 -s "," -S servername -d dbname -i %1 -o temp.txt
set counter=0
for /f %%a in (temp.txt) do set /a counter+=1
echo ^^!total rows=%counter% >> %2
echo !str1! >> %2
echo !str2! >> %2
for /F "delims=¶" %%i in (temp.txt) do ( echo %%i >> %2 )
Please help me, I’m going insane over this ridiculous little problem.
You might try
in your batch script prior to invoking sqlcmd. It wouldn’t be entirely intuitive, but perhaps it plays a role.
If all else fails, get your self a version of
bomstrip, and you should be in the clear.HTH
Update
I have a ‘fixed’ version for windows, that will reopen stdin/stdout in binary mode, so that you’ll avoid getting line-ends converted for you automatically (sic!):
Now you can do: