使用批处理文件为所有子文件夹中的所有文件名添加前缀

斯林克

在此先感谢您的帮助。我有一个文件夹,里面有大量随机命名的子文件夹。在这些子文件夹中有许多带有编号文件名的文件。例如..

\ParentFolder\UniqueFolderName\0001.ogg
\ParentFolder\UniqueFolderName\0002.ogg
\ParentFolder\UniqueFolderName\0003.ogg
\ParentFolder\DifferentFolderName\0001.ogg
\ParentFolder\DifferentFolderName\0002.ogg
\ParentFolder\DifferentFolderName\0003.ogg

我想从 \ParentFolder\ 运行一个批处理文件,它可以重命名 .ogg 文件以继承唯一的文件夹名称,并以这样的方式结束......

\ParentFolder\UniqueFolderName\UniqueFolderName - 0001.ogg
\ParentFolder\UniqueFolderName\UniqueFolderName - 0002.ogg
\ParentFolder\UniqueFolderName\UniqueFolderName - 0003.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0001.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0002.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0003.ogg

这可能是超级简单的东西。但是如果不将批处理文件放在每个子文件夹中,我的小脑袋就无法弄清楚如何做到这一点。

莫菲

这可以通过以下带注释的批处理代码来完成:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Run in a separate command process started by FOR with cmd.exe /C the
rem command DIR to get a list of non-hidden *.ogg files in bare format
rem with just full qualified file name (drive + path + name + extension)
rem in specified directory and all its subdirectories.

rem This list is captured by FOR and processed line by line with disabling
rem the default line splitting behavior of __FOR__. The subroutine RenameFile
rem is called with each full qualified file name enclosed in double quotes.

for /F "delims=" %%I in ('dir "\ParentFolder\*.ogg" /A-D-H /B /S 2^>nul') do call :RenameFile "%%I"

rem Restore initial environment resulting in deletion of all
rem environment variables used in the subroutine below.
endlocal

rem Exit processing of this batch file if command extensions
rem are enabled as by default and expected by this command line.
rem Secure would be the usage of command EXIT. But that makes it impossible
rem to see error messages on running this batch file from within a command
rem prompt window and makes it also impossible to call this batch file from
rem another batch file. Secure would be also to jump with GOTO to a label
rem at end of the batch file below the subroutine RenameFile.
goto :EOF


:RenameFile
rem Get just file name without drive, path and extension.
set "FileName=%~n1"

rem Get just drive and path of file name ending with a backslash.
set "FilePath=%~dp1"

rem Get name of folder containing the file.
for %%J in ("%FilePath:~0,-1%") do set "FolderName=%%~nxJ"

rem Exit the subroutine if file name contains already the folder name.
echo "%FileName%" | %SystemRoot%\System32\find.exe /I "%FolderName% - " >nul 2>nul
if not errorlevel 1 goto :EOF

rem Exit the subroutine if there is already a file or folder
rem with new file name in the folder of the current file.
if exist "%FilePath%%FolderName% - %FileName%%~x1" goto :EOF

rem Rename the file by prepending it with folder name, space, hyphen, space.
ren %1 "%FolderName% - %FileName%%~x1"
goto :EOF

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

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • find /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • ren /?
  • setlocal /?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

批处理文件,用于获取文件夹中的所有文件名

Windows批处理文件:转换子文件夹中的所有文件

如何在所有文件夹和子文件夹中添加前缀?(视窗)

在批处理文件的所有子文件夹中运行命令

在所有子文件夹中运行批处理文件

使用批处理文件删除所有子文件夹?

如何清除而不删除所有子文件夹中的所有文件(批处理文件除外)?

为 ls(list 命令)输出目录中的所有文件添加前缀文件名

如何在文件夹中的所有文件和文件夹中添加前缀?(视窗)

如何对文件夹中的所有文件运行批处理文件?

使用所有文件名的列表,输出缺少的文件名(如果文件夹中未包含)(批处理脚本)

Tesseract-OCR可以在Windows cmd的所有子文件夹中批处理文件?

批处理文件以启动XP开始文件夹中的所有程序

通过批处理文件从子文件夹中删除所有内容

使用批处理文件向批量文件名添加前缀时出现问题

将批处理脚本中变量中的文件夹内的所有文件名串联起来

批处理文件,删除目录中除最新文件夹外的所有文件夹

使用批处理文件删除文件夹中除一个扩展名(例如.idf)以外的所有文件

如何使用批处理文件以编号顺序列出文件夹中特定扩展名的所有文件

使用C#将前缀添加到具有特定扩展名的文件夹中的所有文件名

批处理文件以在文件夹中创建新的子文件夹,将文件移动到目录中所有文件夹的新创建的子文件夹

批处理文件以使用计时器执行文件夹中的所有.exe

如何使用批处理文件删除2个特定文件夹中的所有内容?

如何重命名(通过添加前缀)从当前文件夹开始的所有文件和文件夹?

批处理文件:如何将所有.doc文件备份到文件夹

如果在子文件夹中多次找到文件 - 使用批处理脚本删除所有文件

将文件夹及其子文件夹中的所有文件名写入CSV文件的脚本

使用批处理删除文件夹中除列表中的文件以外的所有文件

批处理文件:复制除一个文件夹外的所有文件和文件夹