For some back info to help you guys out, I'm making a small program that will take an .SQL file and restore it to the users localhost phpMyAdmin using mysql.exe. Importing the file through the MyAdmin itself by hand takes forever as it has to unzip and parse the information, this method is quicker and it's for my coworkers as we're always pushing backups and restores to our WAMP to experiment with.
My one issue I'm facing is that I can't seem to get it to change directories no matter what. I understand that wherever the executable was launched is the path that the system will inherit, so why can't I simply tell it to change directories?
Here's my code so far:
system("cd c:\wamp64\bin\mysql\mysql5.7.14\bin");
system("PAUSE");
system("dir");
system("PAUSE");
system("mysql.exe -u root -p dataBase < tables.sql");
Note: The pauses and system("dir") are in there strictly for testing purposes, once the window spits back the correct contents of the directory, then I know that it works and I'll only have the first and last lines left in the program.
UPDATE: I've been notified that every call to system creates a new CMD process which makes perfect sense as to why the cd will not effect anything, as it's opening a new process, changing directories, then on the next line it's calling a new process and pausing that. What would be the workaround for this?
UPDATE 2: I decided to join multiple commands together using && but that limits me on the usability and "styling" side of things, but that's not a problem, the program is needed as a run & done type of deal.
Here's what I've come up with:
system("cd c:\\wamp64\\bin\\mysql\\mysql5.7.14\\bin && mysql.exe -u root -p dataBase < table.sql");
cout << "\n" << "The database 'dataBase' has been restored using 'table.sql' successfully!" << "\n\n";
return 0;
Are there any advantages to creating a .cmd file and running it using CreateProcess() or disadvantages to using the method that I've come up with?
Note: The database name and .sql files aren't pulled from anything, the names will be static so I don't have to worry about updating that.