【问题标题】:CMD to iterate and recursively rename all filenames and append the current subdirectory folder to the current filenameCMD 迭代并递归重命名所有文件名并将当前子目录文件夹附加到当前文件名
【发布时间】:2021-02-28 06:29:48
【问题描述】:

cmd中的这个命令重命名当前目录下的所有文件

for /f "tokens=*" %a in ('dir /b') do ren "%a" "00_%a"

但我需要获取当前子目录名称(仅文件夹名称)并将其附加到 %a

它应该遍历parent 文件夹中的所有子目录。

我的路径 C:\Temp\Photos_ToRename 包含以下文件夹和文件

NVA-1234(这是一个子目录名称)
--- IMG_0999.jpg(重命名为 NVA-1234_IMG_0999.jpg)
--- IMG_0989.jpg(重命名为 NVA-1234_IMG_0989.jpg)
--- IMG_0979.jpg(重命名为 NVA-1234_IMG_0979.jpg)

NVS-3456(这是子目录名称)
--- IMG_1999.jpg(重命名为 NVS-3456_IMG_1999.jpg)
--- IMG_1989.jpg(重命名为 NVS-3456_IMG_1989.jpg)
--- IMG_1979.jpg(重命名为 NVS-3456_IMG_1979.jpg)

NVS-3359(这是子目录名称)
--- IMG_2999.jpg(重命名为 NVS-3359_IMG_2999.jpg)
--- IMG_2989.jpg(重命名为 NVS-3359_IMG_2989.jpg)
--- IMG_2979.jpg(重命名为 NVS-3359_IMG_2979.jpg)

.....

【问题讨论】:

  • 我最近在这里回答了一个类似的问题:stackoverflow.com/a/64601339/1024832。它基本上通过将目录名称移动到文件名中来扁平化目录结构,我认为这正是您想要做的。如警告中所述,仅在您拥有副本/备份的目录上运行它。
  • 我将batch-rename 更改为batch-file,因为我认为这就是您的意思;如果我错了并且您不想使用批处理文件,只需删除该标签...
  • DOS 没有for /f。这是 Windows cmd 中的一个命令,即completely different thing。当你在谈论 Windows cmd 时不要使用 DOS

标签: windows batch-file cmd file-rename


【解决方案1】:

这应该可以在没有字符串操作的情况下完成这个技巧,并且只需要迭代所需的目录层次结构深度 - 用于在命令提示符中直接使用的代码:

for /D %J in ("C:\Temp\Photos_ToRename\*") do @pushd "%~J" && ((for /F "delims= eol=|" %I in ('dir /B /A:-D-H-S "*.jpg"') do @ren "%I" "%~nxJ_%I") & popd)

批处理文件中的使用代码(使用 cmets):

@echo off
rem // Iterate only the required directory hierarchy depth:
for /D %%J in ("C:\Temp\Photos_ToRename\*") do (
    rem // Change into the currently iterated sub-directory:
    pushd "%%~J" && (
        rem /* Iterate through matching files; the `for /F` loop together with `dir` is
        rem    mandatory here and cannot be replaced by a simple standard `for` loop like
        rem    `for %%I in ("*.jpg") do`, because then, files might become renamed twice
        rem    since `for` does not build the complete file list prior to looping, but
        rem    `for /F` does as it awaits the full output of the `dir` command: */
        for /F "delims= eol=|" %%I in ('dir /B /A:-D-H-S "*.jpg"') do (
            rem // Actually rename the currently iterated file:
            ren "%%I" "%%~nxJ_%%I"
        )
        rem // Return from sub-directory:
        popd
    )
)

【讨论】:

    【解决方案2】:

    我认为这应该可以解决问题:

    @echo off
    set root=%~dp0
    for /f "tokens=*" %%a in ('dir /b /s') do call :process "%%a"
    exit /b
    
    :process
    set fullfolder=%~dp1
    call set folder=%%fullfolder:%root%=%%
    if not %folder%x==x call set folder=%%folder:\=_%%
    set newfile=%folder%%~nx1
    ren %1 %newfile%
    

    这会将每个文件保留在其当前文件夹中,但将其重命名为 folder_subfolder_filename。

    我必须添加 if,否则会使用空文件夹变量将 \=_ 分配给它。

    我首先存储当前路径以便从每个文件的完整路径中删除它,然后添加名称的相对路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 2016-01-31
      • 2021-05-27
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多