Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定Windows批处理脚本中的动态文件名?_Windows_Batch File_Cmd_Zip_7zip - Fatal编程技术网

确定Windows批处理脚本中的动态文件名?

确定Windows批处理脚本中的动态文件名?,windows,batch-file,cmd,zip,7zip,Windows,Batch File,Cmd,Zip,7zip,我们在一个固定目录中有一个文件名为database-x.x.x.zip,其中x.x.x是一个可以更改的版本号 我们想创建一个将解压此文件的批处理脚本-我们可以使用7zip从批处理脚本解压文件,但我们需要能够将文件名传递给7zip-如果文件名不是常量,我们如何确定批处理脚本中的文件名 脚本需要解压缩存档,然后运行存档中的ant文件: "C:\Program Files\7-Zip\CommandLine\7za.exe" x %FILE% ant -f %UNZIPPED_ARCHIVE_DIR%

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

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

脚本需要解压缩存档,然后运行存档中的ant文件:

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

使用奇妙的for循环

从命令行

for /f "tokens=*" %f in ('dir database-?.?.?.?.zip /b') do 7za xxx %f
从批处理文件

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

其中xxx是传递到7zip的选项

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

您可能还需要添加一些检查(在
7z…
行之前),以确保该文件夹不存在,如果存在,请执行一些操作

powershell Expand-Archive x.x.x.zip "where_you_want_to_unzip"

如果您需要使用7zip来解压,那么我无法帮助您通过批处理文件解压,我总是使用它。

可能有一个或两个多问号,还有?仅匹配单个字符(版本号可以很容易地为多个数字)