Misha Moroshko
Misha Moroshko

Reputation: 171321

Need help with writing a batch file

I'm writing a batch file on Windows which gets one argument: a zip file name that was created with 7-zip.

This file contains a single MySQL backup file inside, say backup.sql.

I would like to restore the database given this zip file.

So, first I extract it to a temporary directory like this:

path_to_7zip\7z e "%~1" -otemp_dir

And now is my question: How could I know the name of the extracted file (i.e. backup.sql) ?

I would like to write something like this:

path_to_mysql\mysql < extracted_file_name

(where extracted_file_name should be backup.sql)

What should I write instead of extracted_file_name if I don't know the file name that is inside the zip file ?

Upvotes: 0

Views: 161

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126777

You could extract the file in an empty directory, where you could be sure that it's the only present file. Then you could use the for instruction to gather such name in a variable and use it as you prefer.

FOR /F "tokens=*" %%F IN ('dir /b *.sql') DO path_to_mysql\mysql < %%F

Notice that, if there's more than one SQL file in the archive, all of them will be processed. I think this could be a bonus, but it is undesirable I think you could simply jump out of the FOR with a GOTO.


Edit: using only the /F form of FOR I forgot that the "normal" FOR does already the job we need.

FOR %%F IN (*.sql) DO path_to_mysql\mysql < %%F

Notice that all this stuff works only if the current directory is the extraction directory, otherwise you have to change the expressions in the parentheses accordingly.

Bonus links: reference for FOR and FOR /F - the only commands that do something useful in batch - and that obviously have an impossible syntax. :D

Upvotes: 1

Benoit
Benoit

Reputation: 79155

I think 7z has an “l” command to list contents of an archive. pipe this output to mysql.

Upvotes: 0

Related Questions