tags:

views:

126

answers:

3

We have a single file in a fixed directory that will have the name database-x.x.x.zip where x.x.x is a version number that can change.

We want to create a batch script that will unzip this file - we can use 7zip to unzip the file from a batch script but we need to be able to pass the file name to 7zip - how can we determine the file name in a batch script given that the file name will not be constant?

Update

Realized I should provide more details.. script needs to unzip an archive and then run an ant file which was in the archive:

"C:\Program Files\7-Zip\CommandLine\7za.exe" x %FILE%
ant -f %UNZIPPED_ARCHIVE_DIR%\db.xml 
A: 

use the fantastic for loop

from the command line

for /f "tokens=*" %f in ('dir database-?.?.?.?.zip /b') do 7za xxx %f

from the batch file

for /f "tokens=*" %%f in ('dir database-?.?.?.?.zip /b') do 7za xxx %%f

where xxx is the options passes to 7zip

Preet Sangha
Perhaps one two many question marks are there and ? only matches a single character (version numbers could easily be multiple digit)
Rudu
A: 

Not exactly an answer to your question but it is possible to use wildcards when specifying name of the archive to unzip.

Example:

7z.exe e c:\temp\*.zip
Marek Grzenkowicz
Ah, didn't realize that. But.. we need to unzip and then run an ant script on one of the unzipped files.. believe we'll need the full path for that.. let me update the post..
Marcus
A: 

Source: extract.bat:

@echo off
::: extract.bat - Extract all database-*.zip files in the given folder
::: usage: extract.bat [folder]
:::     folder - Search for files here (defaults to current folder)

set search=database-*.zip /b
if "%~1" neq "" set search=%1\%search%

for /f "tokens=*" %%f in ('dir %search%') do (
  7z x %%f %%~nf
  ant -f %%~nf\db.xml
)

If you really need to exclude database zips that don't follow the version folder format (eg if there's one called database-old.zip and that shouldn't be extracted) then you'll need to find a regex matcher for the command line in Windows - which is possible. That -or- if you keep your version numbers down to a single digit, then you can use the ? single character match.

You may also want to add some checking (before 7z... line) to make sure he folder doesn't already exist and do something if it does.

Rudu