如何将目录中的每个* .7z文件重命名为每个7-Zip存档文件中* .cue文件的名称?

时间:2017-03-03 23:47:42

标签: batch-file rename 7zip

我的问题与:Batch Rename contents of ZIP file to ZIP file name

有关

但我正在寻找一个更简单的批处理文件,因为我不太了解它。

我有大约600个.7z文件。我希望这些7z文件名与每个.7z文件中包含的.cue文件相匹配。

为了更清楚,我举一个例子:

文件Crash Bandicot PSX 1995.7z包含:

  • Crash Bandicot(USA)track 1.bin
  • Crash Bandicot(USA)track 2.bin
  • Crash Bandicot(USA)track 3.bin
  • Crash Bandicot(美国).cue

我想重命名.7z名称以匹配.cue文件(最好)。像这样:

Crash Bandicot (USA).7z仍然包含:

  • Crash Bandicot(USA)track 1.bin
  • Crash Bandicot(USA)track 2.bin
  • Crash Bandicot(USA)track 3.bin
  • Crash Bandicot(美国).cue

有人可以帮我制作批量来做这件事吗?

编辑:
这是我到目前为止的脚本代码:

FOR /r %%i IN (*) DO "C:\Program Files (x86)\7-Zip\7z.exe" a "%%~ni.7z" "%%i"

1 个答案:

答案 0 :(得分:0)

首先,我建议在 7-Zip 的程序文件目录中打开帮助文件7zip.chm,然后双击。在目录标签上打开列表项命令行版,然后在列表项命令旁边,然后点击 l(列表)打开列出存档文件内容的帮助页面。

下面的批处理文件使用 7-Zip l命令以及开关-i(包含)和-slt(显示技术信息)。批处理文件希望在每个* .7z存档文件的根目录中找到* .cue文件,因此不会使用该选项以递归方式搜索存档中的* .cue文件。

其次,在包含大约600 * .7z存档文件的目录中打开命令提示符窗口并运行命令行:

"%ProgramFiles(x86)%\7-Zip\7z.exe" l -i!*.cue -slt "Crash Bandicot PSX 1995.7z"

7-Zip 输出档案Crash Bandicot PSX 1995.7z内的* .cue文件列表,并显示技术信息。现在知道 7-Zip (我使用的版本16.04)的输出是怎样的,批处理文件中用于第二个 FOR 循环的选项可以更好地理解

下面的批处理文件仅在当前目录中搜索* .7z文件,如果名称不匹配,则重命名包含* .cue文件的所有文件。

在{。{1}}(裸格式)之后插入 DIR 选项/S(也在子目录中搜索),如果* .7z文件不在当前目录中,但是在当前目录及其子目录中。

以下是此归档文件重命名任务的注释批处理文件:

/B

首先在批处理文件附近插入重命名命令@echo off setlocal EnableExtensions DisableDelayedExpansion rem Prepend folder path to 7z.exe temporarily to local copy of environment rem variable PATH to be able to call 7z.exe without full path enclosed in rem double quotes as this would make the FOR command line running 7z.exe rem really very difficult. set "PATH=%ProgramFiles(x86)%\7-Zip;%PATH%" set "PauseOnWarning=0" rem Search in current directory for *.7z files using command DIR and not FOR rem directly and call for each found file the subroutine RenameArchiveFile rem with file name enclosed in double quotes. It is important not using FOR rem for searching for *.7z files as the found *.7z files are renamed while rem executing the loop. A *.7z file could be easily processed more than once rem on using command FOR directly. Better is in this case to use command DIR rem which first search for all *.7z files (including hidden ones which FOR rem would not do) and then outputs the names of all found files being rem processed next by command FOR. So it does not matter that file names rem change while FOR processes the list of file names output by DIR. for /F "delims=" %%I in ('dir /A-D /B *.7z 2^>nul') do call :RenameArchiveFile "%%I" rem Output an empty line and halt batch file execution if any warning rem was output while processing the *.7z files in the subroutine. if %PauseOnWarning% == 1 echo/ & pause rem Restore previous command line environment and exit batch processing. endlocal goto :EOF rem RenameArchiveFile is a subroutine called with name of the archive file rem enclosed in double quotes. The file name can be with or without path. rem 7-Zip is executed to output the list of *.cue files with showing rem technical information for easier parsing the output lines. Any error rem message output by 7-Zip is suppressed by redirecting them from STDERR rem to device NUL. It is not expected that 7-Zip outputs an error at all. rem The first 14 lines are always skipped by command FOR. The next lines rem are split up into two substrings using space and equal sign as string rem delimiters. The first substring is assigned to loop variable A and rem everything after first substring and 1 to n spaces/equal signs is rem assigned to loop variable B. There is hopefully no *.cue file which rem begins unusually with an equal sign or a space character. rem If the first substring (token) from current line assigned to loop rem variable A is case-sensitive the word Path, it is expected that the rem second substring (token) assigned to loop variable B is the name of rem the *.cue file found in the current archive file. In this case the rem file name is assigned to an environment variable and loop is exited rem with a jump to label HaveFileName. rem A warning message is output if no *.cue file could be found in archive. :RenameArchiveFile for /F "skip=14 tokens=1* delims== " %%A in ('7z.exe l -i!*.cue -slt %1 2^>nul') do ( if "%%A" == "Path" ( set "FileName=%%B" goto :HaveFileName ) ) echo Warning: Could not find a *.cue file in: %1 set "PauseOnWarning=1" goto :EOF rem A *.cue file was found in current archive. The file extension cue rem at end is replaced by 7z for the new name of the archive file. rem First it is checked if the current archive file has already the file rem name of the *.cue file inside the archive in which case the subroutine rem RenameArchiveFile is exited resulting in processing of batch file being rem continued on main (first) FOR loop. rem If there is no *.7z file in directory of current archive file with rem the new name, the current archive file is renamed to new name and rem subroutine RenameArchiveFile is exited without further processing. rem But if a different archive file than current archive file has already rem the new archive file name, the subroutine outputs a 3 lines warning rem and exits without renaming the current archive file. The user has to rem handle this file name collision. :HaveFileName set "FileName=%FileName:~0,-3%7z" if "%FileName%" == "%~nx1" goto :EOF if not exist "%~dp1%FileName%" ren %1 "%FileName%" & goto :EOF echo Warning: Could not rename %1 echo to "%FileName%" echo because a file with that name already exists. set "PauseOnWarning=1" goto :EOF 左侧的命令echo,并在renpause之间的行中插入endlocal,这可能是个好主意。 goto :EOF测试* .7z文件在没有真正重命名的情况下将如何重命名。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • 7z ... 7-Zip没有使用标准的Windows语法作为选项,但是在没有任何参数的情况下输出了一个简短的帮助,或只是 -h 作为参数。
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?

另请阅读Microsoft文章Using Command Redirection Operators,了解此批处理文件中使用2>nul两次的解释,并使用插入符号>转义重定向运算符^以获取{ {1}}解释为Windows命令解释器解析 FOR 命令行时的文字字符,稍后由 FOR 执行 DIR 命令行作为重定向操作

如果没有出现在双引号字符串中,请参阅Single line with multiple commands using Windows batch file以了解&符号>在命令行中的含义。