Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
Batch file 如何将目录中的每个*.7z文件重命名为每个7-Zip存档文件中的*.cue文件名?_Batch File_Rename_7zip - Fatal编程技术网

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

Batch file 如何将目录中的每个*.7z文件重命名为每个7-Zip存档文件中的*.cue文件名?,batch-file,rename,7zip,Batch File,Rename,7zip,我的问题涉及: 但我正在寻找一个更简单的批处理文件,因为我不太了解它 我有大约600.7z文件。我希望这些7z文件名与每个.7z文件中包含的.cue文件匹配 为了更清楚,我举了一个例子: 文件Crash Bandicot PSX 1995.7z包含: 碰撞带(美国)轨道1.5 撞车班迪科特(美国)赛道2.5号 撞车班迪科特(美国)赛道3.5码 撞车班迪科特(美国)。提示 我想重命名.7z名称以匹配.cue文件(最好)。像这样: Crash Bandicot(美国)。7z仍包含: 碰撞带(美

我的问题涉及:

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

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

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

文件
Crash Bandicot PSX 1995.7z
包含:

  • 碰撞带(美国)轨道1.5
  • 撞车班迪科特(美国)赛道2.5号
  • 撞车班迪科特(美国)赛道3.5码
  • 撞车班迪科特(美国)。提示
我想重命名.7z名称以匹配.cue文件(最好)。像这样:

Crash Bandicot(美国)。7z
仍包含:

  • 碰撞带(美国)轨道1.5
  • 撞车班迪科特(美国)赛道2.5号
  • 撞车班迪科特(美国)赛道3.5码
  • 撞车班迪科特(美国)。提示
有人能帮我做一批吗

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

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

首先,我建议双击7-Zip的program files目录中的帮助文件
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输出存档中的*.cue文件列表
Crash Bandicot PSX 1995.7z
,并显示技术信息。现在,了解了7-Zip(我使用的是16.04版)的输出情况后,可以更好地理解批处理文件for secondfor循环中使用的选项

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

如果*.7z文件不全部在当前目录中,而是在当前目录及其子目录中,则在
/B
(裸格式)之后插入DIR选项
/S
(也在子目录中搜索)

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

@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
,然后在
endlocal
goto:EOF
之间的一行插入
pause
,以测试如何在不进行实际重命名的情况下重命名*.7z文件

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 7z
    。。。7-Zip没有对选项使用标准的Windows语法,但在不使用任何参数或仅使用
    -h
    作为参数的情况下输出有关运行的简要帮助
  • dir/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • 暂停/?
  • rem/?
  • ren/?
  • 设置/?
  • setlocal/?
另请阅读Microsoft关于的文章,以获取有关此批处理文件中使用的
2>nul
的解释,该批处理文件使用带插入符号的重定向操作符
进行两次转义,以便在Windows命令解释器解析命令行时将
解释为文字字符作为重定向操作符,由FOR执行DIR命令行


请参阅以了解如果双引号字符串中不存在符号,那么命令行中的符号是什么。

此外,忘了提及不需要解压缩文件,只需重命名即可。谢谢这不是一个编码驾车穿越,在这里你在一个窗口下订单,然后停到下一个窗口取代码。您自己做了哪些工作?这里有一个提示:
7z l*.7z
列出了存档中的文件。从中您可以找到
*.cue
文件并重命名存档。谢谢nico。我会试试的。我以为你想重命名这个文件,现在你说的是提取所有内容并创建一个新的。是哪一个?有人已经告诉您需要什么命令,只需解析它的输出。