Solved

MySQL windows batch file need to escape backslashes in %CD% or convert them to forward slashes

Posted on 2008-06-13
4
1,818 Views
Last Modified: 2010-04-21
Hello,
I have a Windows batch file containing the following command:

mysql -uuser -ppassword thedatabase -e
 "select * into outfile '%CD%\\thetable.csv' FIELDS TERMINATED BY ',' OPTIONALLY
 ENCLOSED BY '\"' ESCAPED BY '' LINES TERMINATED BY '\r\n' from thetable;"

and when it is run I get the error
ERROR 1 (HY000) at line 1: Can't create/write to file 'C:WorkDatabase\thetable.csv' (Errcode: 2)

The current directory is C:\Work\Database and what's happening is MySQL is interpreting the backslashes as an escape character. The solution is to convert %CD% to double the backslashes or substitute a forward slash for them.
0
Comment
Question by:pdegregorio
  • 2
4 Comments
 
LVL 76

Expert Comment

by:arnold
Comment Utility
Replace %CD% with c:\\Work\\database is the easiest.

Define the CD variable as c:\\Work\\database at the beginning of the batch.
0
 
LVL 5

Accepted Solution

by:
Alex-w474 earned 250 total points
Comment Utility
This will help: BAT file tips - http://www.alexatnet.com/node/22
SET FOLDER=%CD%

ECHO %FOLDER:\=\\%

Open in new window

0
 

Author Comment

by:pdegregorio
Comment Utility
Using Alex-w474 suggestion the following works:

mysql -uuser -ppassword thedatabase -e
 "select * into outfile '%CD:\=\\%\\thetable.csv' FIELDS TERMINATED BY ',' OPTIONALLY
 ENCLOSED BY '\"' ESCAPED BY '' LINES TERMINATED BY '\r\n' from thetable;"

Thank you!
0
 

Author Closing Comment

by:pdegregorio
Comment Utility
Very nice, elegant solution. Thanks for the speedy reply.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now