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
  • 2
LVL 77

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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