views:

1159

answers:

4

I've got some batch files that I use to help automate the process of creating and reloading development databases. It makes sense to create and maintain these batch files in Visual Studio (i.e., in a VS Database project). They look pretty simple, like this:

@echo off
echo Setting server and db from defaults.
set SERVERNAME=(LOCAL)
set DB=PLEDGES
echo Creating tables on server %SERVERNAME% and database %DB%
sqlcmd -S %SERVERNAME% -d %DB% -E -i DropAllTables.sql
sqlcmd -S %SERVERNAME% -d %DB% -E -i dbo.UserType.Table.sql
sqlcmd -S %SERVERNAME% -d %DB% -E -i dbo.RegisteredUser.Table.sql
echo Done creating tables.

The problem is that when I run them, this is the output:

C:\>
'' is not recognized as an internal or external command,
operable program or batch file.
Creating tables on server (LOCAL) and database PLEDGES
Done creating tables.

In other words, Visual Studio is somehow invisibly inserting a set of characters that the Windows command processor is interpreting as a command, i.e., "". A Google search returned nothing on this. Has anybody else run into this? Thoughts on a fix?

I've got a workaround (just add a blank line to the beginning of each file, and it'll show the error, but otherwise work fine), but the anal-retentive side of myself gets annoyed each time I run one of these files.

+9  A: 

What is happening is that VisualStudio is being clever and hiding you from the fact that your batch-file has been saved in a non-ASCII character encoding (e.g. your file is UTF-8 or some other non-ASCII encoding).

My project team has been caught out a few times by this (if the files are checked into CVS it makes the files a mess).

I tend to use Notepad++, look at the encoding on the lower-right (it should say that it is ANSI), if you need to change it go to the format menu and change the type then save.

Visual Studio should look identical but the file size should have halved!

Ray Hayes
Note: The encoding is called UTF-8, not UTC-8
Joey
@Johannes, thanks for pointing that out, I have corrected it.
Ray Hayes
Thanks -- Notepad++ was the right tip. Odd that Visual Studio and cmd.exe don't work together on something as basic as a text file. Huh.
Ken Smith
See also http://techencoder.com/index.php/2009/10/batch-file-formatting/ (screenshots of the solution described)
Robert Claypool
A: 

Wow. A modern IDE and it's randomly trashing your .bat file. Be sure to inform the press.

A more likely scenario is that Visual Studio is not trashing your .BAT file. It is not inserting random data, but rather a Unicode byte-order mark. Save your .BAT file as ANSI.

John Saunders
+4  A: 

Ray's answer is the right answer. Specifically, Visual Studio puts a Byte Order Mark at the beginning of files that have non-ANSI characters, and as far as I can tell, it puts that as the first part of every XML file.

shapr
+10  A: 

Ray is correct when he says that Visual Studio's default format for text is something like UTF-8. Although Notepad++ is a great tool and I use it myself, there is an alternative in that you can tell Visual Studio to store your text file in ASCII format:

In VS2008, select your file in the solution explorer and choose File...Save myfile.bat As...

On the down arrow on the Save button, choose Save with Encoding.

When saving in the Advanced Save Options dialog, Choose US-ASCII in the Encoding drop-down list. Set the line endings as required, or leave it as Current Setting.

jadusty
That's helpful, thanks.
Ken Smith
Thank you for my first badge - glad to be of service.
jadusty