1

Background

I have a batch file on Windows XP which takes one argument: a zip file name that was created with 7-zip.

This file contains a single MySQL backup file inside: backup.sql.

I would like to restore the database given this zip file. First I extract it to a temporary directory like this:

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

I would like to have something like this (where extracted_file_name would be backup.sql):

path_to_mysql\mysql < extracted_file_name

Question:

How do I work out the name of the extracted file (i.e. backup.sql)?

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

2 Answers 2

3

You could create a batch file containing the below steps.

cd\7ztemp
del \7zout\*.* /q
7z e *.7z -o\7zout
cd\7zout
for %%f in ( *.sql) do mysql ^< %%f

Assumptions that I have made:

  • 7ztemp is the directory to which the file is downloaded, and contains no other files
  • 7zout is the directory to which the 7z file is extracted to. I am deleting existing files in 7zout before extracting the archive
5
  • Thanks ! One little question: what does ^ means in ^< %%f ? Oct 1, 2010 at 15:41
  • @Misha the < is a redirection operator, and needs to be escaped so that it works correctly. ^ is the escape character on the command prompt
    – Sathyajith Bhat
    Oct 1, 2010 at 15:46
  • Also, once you're happy that the script works fine you could add @echo off as the first line to suppress the commands being shown
    – Sathyajith Bhat
    Oct 1, 2010 at 15:47
  • I saw many times that < is used without ^. Can you show an example where it won't work properly without ^ ? @echo off is a good idea ! Thanks for your time ! Oct 1, 2010 at 15:55
  • @Misha Here's a screenshot of what happened while I was testing the script. It might be localized to me, but I wasn't sure i.imgur.com/TG3pe.png (ps: you can ignore that message about mysql not being a valid commad, I don't have it installed)
    – Sathyajith Bhat
    Oct 1, 2010 at 16:20
0

If there is only one file in the folder then you could use DIR /B to list the contents.

The only entry would be the file that has been decompressed.

4
  • How should I use DIR /B ? path_to_mysql\mysql < DIR /B does not work... Oct 1, 2010 at 12:48
  • There are quite a lot of hits on Google for the search "output of a command in a variable dos" but all the examples I've tried don't work for me. Maybe it will for you?
    – Richard
    Oct 1, 2010 at 13:24
  • Misha, forget trying to get it all working in one command.try dir /b c:\windows It lists the contents of a directory just showing filenames. So you could list the contents of a directory with one file and it justs lists that one file.
    – barlop
    Oct 1, 2010 at 13:45
  • You don't even need to output a command to a variable. Just extract the zip to a directory, then COPY the file you want to the directory you want. If you don't know the filename, it doesn't matter. You can copy the entire contents of the extracted zip. Or, *.sql, to a directory. Don't need to do it in one command!
    – barlop
    Oct 1, 2010 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.