我想批量使用此代码。第一步是输入源文件夹,其树形结构如下所示:
我想要实现的是将源文件夹内的每种类型的存档提取到存档所在的同一文件夹中,例如将“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"
答案1
使用 7Zip 递归提取存档文件并将其解压到存档文件所在的文件夹中
您可以使用-o
开关7Zip这将指定 extract 命令提取适用存档文件内容的输出目录的完整路径。
您可以使用为/F循环递归目录命令迭代完整的存档路径并将其传递给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
更多资源
- 目录
-
此外,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
答案2
for /Recursive
为什么不对所有需要的扩展都使用一个呢?
@echo off
For /R "%userprofile%\desktop\test" %%i in (*.Zip,*.7z,*.Rar
)do "c:\program files\7-zip\7z.exe" x "%%~dpnxi" -o"%%~dpi\"
goto :EOF
FOR /R
Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path : The folder tree where the files are located.
set : A set of one or more files enclosed in parentheses (file1.*, another?.log).
Wildcards must be used.
If (set) is a period character (.) then FOR will loop through every folder.
command : The command to carry out, including any parameters.
This can be a single command, or if you enclose it
in (parentheses), several commands, one per line.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.
If the [drive:]path are not specified they will default to the current drive:path.