0

I need some help. I want to get the database name in MySQL to make it as directory name. But my code is not correct . it fails.

e.g
DATABASE NAME 
- sample
- sample_test

i want to create a directory with the same name as database name.

my code is:

@echo off
SET path=C:\Users\neca\Desktop
cd C:\wamp\bin\mysql\mysql5.5.24\bin
SET UserName=root
SET UserPass=root

mysql -u%UserName% -r%UserPass% -N -B -e "SHOW DATABASES LIKE '%%sample%%'" |
FOR /F %%D IN ('C:\Windows\System32\findstr /V "information_schema performance_schema"') DO @echo %%D 
IF exist %path%\%%D (echo %%D exist) 
ELSE (mkdir %path%\%%D && echo %%D created)

i got this result: i add pause command to pause the terminal and the display is

sample IF exist C:\Users\neca\Desktop\sample (echo sample exist) 
ELSE  (mkdir C:\Users\neca\Desktop\sample sample created)
Press any key to continue . . .

when i test it just like this the output is correct

 @echo off
 SET path=C:\Users\neca\Desktop
 cd C:\wamp\bin\mysql\mysql5.5.24\bin
 SET UserName=root

 mysql -uroot -N -B -e "SHOW DATABASES LIKE '%%sample%%'" 
 pause

output:

 sample
 sample_test
 Press any key to continue . . .
5
  • It fails how? Any errors? Commented Feb 10, 2015 at 8:58
  • i add pause command to pause the terminal and the display is sample IF exist C:\Users\neca\Desktop\sample (echo sample exist) ELSE (mkdir C:\ Users\neca\Desktop\sample sample created) Press any key to continue . . . @GolezTrol Commented Feb 10, 2015 at 9:03
  • 1
    First, do not change the system environment path variable not even temporarily; use another variable name, e.g. SET "pathX=C:\Users\neca\Desktop" and read again Loop command: against the results of another command. Then you could try FOR /F "usebackq tokens=*" %%G IN (``command with apostrophes here``) DO (code snippet) and note that code snippet in parentheses could consist of more than one command lines (only one backquote, I can't escape it!) Commented Feb 10, 2015 at 9:36
  • Please set echo ON and provide full output from that mysql ..." command (including echoed one). Alone mysql ... without next | piped one. Insert pause immediately behind it instead. Copy a result from CLI window, edit your question and paste it there. Commented Feb 10, 2015 at 16:12
  • when i test it just like this the output is correct @echo off SET path=C:\Users\neca\Desktop cd C:\wamp\bin\mysql\mysql5.5.24\bin SET UserName=root mysql -uroot -N -B -e "SHOW DATABASES LIKE '%%sample%%'" pause output: sample sample_test Press any key to continue . . . @JosefZ Commented Feb 11, 2015 at 1:25

1 Answer 1

1

Under next circumstances:

mysql -uroot -N -B -e "SHOW DATABASES LIKE '%sample%'" 
sample
sample_test
Press any key to continue . . .

this batch script should work:

 @echo off
 pushd C:\wamp\bin\mysql\mysql5.5.24\bin
 SET "pathxxx=C:\Users\neca\Desktop"
 SET "UserName=root"
 for /F "usebackq tokens=*" %%G in (
        `mysql -uroot -N -B -e "SHOW DATABASES LIKE '%%sample%%'"`
    ) do (
          echo processing "%%G"
          IF exist "%pathxxx%\%%~G" (
              echo %%G exist 
          ) ELSE (
              echo mkdir "%pathxxx%\%%~G"
              echo %%G created
          )
    )
 popd

Notes:

  • do not change the system environment variable path, not even temporarily; use another variable name, here pathxxx;
  • instead of chdir command used pushd .. popd pair: popd will change directory back to the path/folder most recently stored by the pushd command;
  • usebackq forces alternate quoting style in for /F command; essential as your command contains ' single quotes;
  • as a matter of general principle, used %%~G with the ~ argument modifier to remove surrounding quotes (") if any;
  • echo mkdir "%pathxxx%\%%~G" for debugging purposes only (get rid of the echo word when debugged.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.