How to bulk add folder name to file name?

2019-03-04 13:53发布

问题:

I have a folder struktur like this:

/1/1/master.jpg
/1/2/master.jpg
/1/3/master.jpg
/2/1/master.jpg
/2/2/master.jpg
/2/3/master.jpg
...

I need to import all images to a website, but the file name have to differ to each other, so I cannot import two (or) more files with the same name. Just numerating the images to master1.jpg, master2.jpg, ... with e.g. AntRenamer is no proper solution, because the image paths/names are assigned to an item number in a csv file I also need to import.

So: How can I bulk add the folder names to the files like this?

/1/1/1_1_master.jpg
/1/2/1_2_master.jpg
/1/3/1_3_master.jpg
/2/1/2_1_master.jpg
/2/2/2_2_master.jpg
/2/3/2_3_master.jpg
...

Thanks for your help! Timo

回答1:

@echo off
setlocal EnableDelayedExpansion
cd C:\Parent\Folder\OfFirstNumberedFolders
for /F "delims=" %%a in ('dir /B /S /A-D master.jpg') do (
   set "fullName=%%a"
   for /F "tokens=1-3 delims=\" %%b in ("!fullName:%CD%=!") do (
      ECHO ren "%%a" "%%b_%%c_%%d"
   )
)


回答2:

Try this in Windows. Remove the echo to make it actually perform the rename.

@echo off
for /f "delims=" %%z in ('dir "master.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
echo ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
)
)
)
pause