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)
...
这应该可以解决问题,而无需字符串操作,并且仅迭代所需的目录层次结构深度-代码可直接在命令提示符中使用:
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)
批处理文件中使用的代码(带注释):
@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
)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句