首页 > 解决方案 > 使用批处理文件为所有子文件夹中的所有文件名添加前缀

问题描述

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

\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

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

标签: batch-filecmd

解决方案


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

@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 /?

推荐阅读