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

Posted on 2008-06-13
Last Modified: 2010-04-21
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.
Question by:pdegregorio
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 79

Expert Comment

ID: 21782719
Replace %CD% with c:\\Work\\database is the easiest.

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

Accepted Solution

Alex-w474 earned 250 total points
ID: 21782762
This will help: BAT file tips -

Open in new window


Author Comment

ID: 21782841
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!

Author Closing Comment

ID: 31467077
Very nice, elegant solution. Thanks for the speedy reply.

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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