使用 7-zip 从每个存档中批量提取文件

Ray*_*rth 5 batch extract 7-zip cmd.exe

我想批量使用此代码。第一步是输入源文件夹,哪个树看起来像这样:

在此处输入图片说明

我想要实现的是将源文件夹中的每种类型的存档提取到存档所在的同一文件夹中,例如。“archive.zip”直接复制到“folder1”。

下面有这个代码,但不知道如何设置目标变量。

SET /P "source="

FOR %%F IN ("%source%\*.zip") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.zip"
FOR %%F IN ("%source%\*.7z") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.7z"
FOR %%F IN ("%source%\*.rar") DO "C:\Program Files\7-Zip\7z.exe" x "%source%\%%~nF.rar"
Run Code Online (Sandbox Code Playgroud)

Vom*_*yle 2

使用 7Zip 从存档文件中递归提取到存档文件所在的同一文件夹

您可以使用7Zip-o的开关,该开关将为 extract 命令指定输出目录的完整路径,以提取适用的存档文件的内容。

您可以使用FOR /F循环和递归DIR命令来迭代完整的存档路径,并使用替换将这些路径相应地传递给7Zip,使其按照您的需要工作。

批处理脚本

@ECHO ON

SET source=C:\Users\User\Desktop\Test
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.zip"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.7z"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.rar"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
EXIT
Run Code Online (Sandbox Code Playgroud)

更多资源

  • 目录
  • 对于/F

    此外,FOR 变量引用的替换也得到了增强。您现在可以使用以下可选语法:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    
    Run Code Online (Sandbox Code Playgroud)