在DOS批处理脚本中确定动态文件名?

时间:2022-02-25 23:32:33

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.

我们在固定目录中有一个文件名为database-x.x.x.zip,其中x.x.x是可以更改的版本号。

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?

我们想要创建一个将解压缩此文件的批处理脚本 - 我们可以使用7zip从批处理脚本解压缩文件,但我们需要能够将文件名传递给7zip - 我们如何在批处理脚本中确定文件名鉴于文件名不会是常数?

Update

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

实现我应该提供更多细节..脚本需要解压缩存档,然后运行存档中的ant文件:

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

2 个解决方案

#1


0  

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

其中xxx是选项传递给7zip

#2


0  

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.

如果你真的需要排除不遵循版本文件夹格式的数据库拉链(例如,如果有一个名为database-old.zip并且不应该提取),那么你需要为命令行找到一个正则表达式匹配器在Windows中 - 这是可能的。那 - 或者 - 如果您将版本号保持为一位数,那么您可以使用?单个字符匹配。

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.

您可能还想添加一些检查(在7z ...行之前)以确保他的文件夹不存在并执行某些操作。

#1


0  

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

其中xxx是选项传递给7zip

#2


0  

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.

如果你真的需要排除不遵循版本文件夹格式的数据库拉链(例如,如果有一个名为database-old.zip并且不应该提取),那么你需要为命令行找到一个正则表达式匹配器在Windows中 - 这是可能的。那 - 或者 - 如果您将版本号保持为一位数,那么您可以使用?单个字符匹配。

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.

您可能还想添加一些检查(在7z ...行之前)以确保他的文件夹不存在并执行某些操作。