如何批量重命名文件为父文件夹的父文件夹名称
How to rename a file to the parent's parent folder name in batch
所以我有一堆文件夹,其目录与以下类似:
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
我想将所有 .png
文件重命名为第一个 randomStringofCharacters
之前的编号。所以基本上
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
有没有批处理脚本可以做到这一点?我试过:
@Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%@ in ("*.png") DO (
Echo Ren: ".\%%~n#\%%@" "%%~n#%%~x@"
Ren "%%@" "%%~n#%%~x@"
)
POPD
)
Pause&Exit
然而这只是重命名父目录的文件。
如果可能的话,有没有办法将所有此类重命名的 .png 文件移动到 .\page\
中新建的文件夹(与 .bat 所在的位置相同),文件夹名称为 page?
提前致谢!
可能是这样的(你必须将主文件夹拖放到批处理中):
@echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
您不应使用 for /R
来处理预定义目录层次深度中的项目:
@echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
请注意,此方法不会检查每个位置是否只有一个 .png
文件。
所以我有一堆文件夹,其目录与以下类似:
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png
我想将所有 .png
文件重命名为第一个 randomStringofCharacters
之前的编号。所以基本上
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
有没有批处理脚本可以做到这一点?我试过:
@Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%@ in ("*.png") DO (
Echo Ren: ".\%%~n#\%%@" "%%~n#%%~x@"
Ren "%%@" "%%~n#%%~x@"
)
POPD
)
Pause&Exit
然而这只是重命名父目录的文件。
如果可能的话,有没有办法将所有此类重命名的 .png 文件移动到 .\page\
中新建的文件夹(与 .bat 所在的位置相同),文件夹名称为 page?
提前致谢!
可能是这样的(你必须将主文件夹拖放到批处理中):
@echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
您不应使用 for /R
来处理预定义目录层次深度中的项目:
@echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
请注意,此方法不会检查每个位置是否只有一个 .png
文件。