简体   繁体   English

从批处理文件中提取 7-zip 文件到它们的包含目录中?

[英]Extract 7-zip files into their containing directory from a batch file?

I have a directory, c:\7zip\test , containing many subdirectories, some containing 7-zip files.我有一个目录c:\7zip\test ,其中包含许多子目录,其中一些包含 7-zip 文件。

I want to find and extract the 7-zip files into their holding directories from a batch file and then delete those 7-zip files.我想从批处理文件中找到 7-zip 文件并将其提取到它们的保存目录中,然后删除这些 7-zip 文件。

@If "%1"=="" (Set pathf=c:\7zip\test\) else (Set pathf=%1) 
@If "%2"=="" (Set lzma2_test=*.7z) else (Set lzma2_test=%2)

for /r "%pathf%" %%f in ("%lzma2_test%") do 7z x -y -mx1 -m0=lzma2 "%%f" -oFolderName
del "%pathf%" + subfolder name (if needed) + 7-zip file name

Instead of FolderName , I should send a directory name, but I do not know how.而不是FolderName ,我应该发送一个目录名称,但我不知道如何。 And the same for del .对于del也是如此。

Open a command prompt , run for /?打开命令提示符,运行for /? and read the output help from top of first page to bottom of last page, especially the section about referencing the loop variable with a modifier like %~dpI with I being the loop variable to reference the drive and path.并阅读从第一页顶部到最后一页底部的输出帮助,尤其是关于使用%~dpI类的修饰符引用循环变量的部分,其中I是引用驱动器和路径的循环变量。

@echo off
if "%~1" == "" (set "pathf=c:\7zip\test") else (set "pathf=%~1")
if "%~2" == "" (set "lzma2_test=*.7z") else (set "lzma2_test=%~2")

for /R "%pathf%" %%I in ("%lzma2_test%") do (
    7z.exe x -y -o"%%~dpI" "%%I"
    if not errorlevel 1 del "%%I"
)

The compression switches -mx1 -m0=lzma2 are useless on extracting an archive.压缩开关-mx1 -m0=lzma2在提取档案时没有用。 The archive contains in header the algorithm used on compression and so extraction works always as long as used 7z.exe supports the archive type and used algorithm on creating the archive.存档在标题中包含用于压缩的算法,因此只要使用的7z.exe支持存档类型和创建存档时使用的算法,提取总是有效的。

The archive file is only deleted if the extraction was successful, ie 7x.exe exited not with a value greater or equal 1 which means equal 0 which is the exit code for a successful extraction.仅当提取成功时才会删除存档文件,即7x.exe退出时的值不大于或等于1 ,这意味着等于0 ,即成功提取的退出代码。

It would be more safe to use the following code which works also if an archive file contains inside another archive file.如果存档文件包含在另一个存档文件中,则使用以下代码会更安全。 This code is recommended also for usage on a FAT16, FAT32 or exFAT drive.此代码也推荐用于 FAT16、FAT32 或 exFAT 驱动器。

@echo off
if "%~1" == "" (set "pathf=c:\7zip\test") else (set "pathf=%~1")
if "%~2" == "" (set "lzma2_test=*.7z") else (set "lzma2_test=%~2")

for /F "eol=| delims=" %%I in ('dir "%pathf%\%lzma2_test%" /A-D /B /S 2^>nul') do (
    7z.exe x -y -o"%%~dpI" "%%I"
    if not errorlevel 1 del "%%I"
)

This code makes sure not extracting an archive file inside an archive file by chance which as it could happen by the first code which iterates of the list of archive files with the list changing on each iteration on extracting an archive file containing an archive file and because of deletion of the successfully extracted archives.此代码确保不会偶然在存档文件中提取存档文件,因为它可能发生在第一个代码迭代存档文件列表的第一个代码中,在提取包含存档文件的存档文件时,列表在每次迭代时发生变化,并且因为删除成功提取的档案。

It would be a good idea to test the used batch file first by running it from within a command prompt window with echo left to 7z.exe and if not errorlevel 1 to see what would be executed without really executing it.最好先在命令提示符窗口中运行它来测试使用的批处理文件,并将echo显留给7z.exeif not errorlevel 1 ,则查看在没有真正执行它的情况下会执行什么。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • call /?
  • del /?
  • dir /?
  • echo /?
  • for /?
  • if /?
  • set /?

Read the Microsoft article about Using command redirection operators for an explanation of 2>nul .阅读有关使用命令重定向运算符的 Microsoft 文章,了解2>nul的说明。 The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line between ' appended as additional arguments.重定向运算符>必须在FOR命令行上使用脱字符^进行转义,以便在 Windows 命令解释器在执行命令FOR之前处理此命令行时解释为文字字符,FOR 使用在后台启动的单独命令进程执行嵌入式dir命令行%ComSpec% /c'之间的命令行作为附加参数附加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM