0

i'm looking to create a batch file which when executed, creates and then runs an sql file against a mysql database. The issue I'm having is that the script uses the > symbol during the query which when used in a SET command wants to dump half my query as a filename.

here is an extract of my batch file:

set FILE=query.sql
set TEXT=SELECT count(*) FROM calls where CallStart < '2012-01-01';
echo %TEXT% >> "%FILE%"

Can anyone help on how I get a batch file to write the < symbol into a file without creating a new export file?

1 Answers1

0

Assuming this is a windows batch file, try escaping the < character with ^:

set TEXT=SELECT count(*) FROM calls where CallStart ^< '2012-01-01';

or use delayed expansion and quote the string:

SETLOCAL enabledelayedexpansion
set FILE=query.sql
set "TEXT=SELECT count(*) FROM calls where CallStart < '2012-01-01';"
echo %TEXT% >> "%FILE%"

I'm not on a windows machine, so I can't test this out for you!

meatballs
  • 3,917
  • 1
  • 15
  • 19
  • Thanks for the comments. When i try to escape the < using ^, it doesn't create the file but it does remove the date from the query within the output file as it thinks thats part of the command (i assume). With the enabledelayedexpansion, this operates the same as without. – user3364870 Feb 28 '14 at 13:30
  • fuller answer here: http://stackoverflow.com/questions/535975/dealing-with-quotes-in-windows-batch-scripts – meatballs Feb 28 '14 at 13:44