1

I have a large (aprox. 150,000) tif files, which all have the same filename. They are only unique because of the directory structure they are held in.

I would like to bulk rename the tiff files so they become unique, based on the directory structre that they are held within.

Does anyone have any method of acheiving this?

I am using Windows Server 2012 so a solution using a cmd script, batch file or windows GUI tool would be perfect.

Ideally, this is what I would like to acheieve, but if I have to have more or all of the directory structure in the final filename thsi would still be very, very helpful.

C:\A_001\B_0001\ABC\0001.tif -> ABC.tif

C:\A_001\B_0001\JKL\0001.tif -> JKL.tif

C:\A_001\B_0001\XYZ\0001.tif -> XYZ.tif

C:\A_001\B_0002\123\0001.tif -> 123.tif

C:\A_001\B_0002\456\0001.tif -> 456.tif

C:\A_001\B_0002\789\0001.tif -> 789.tif
Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • possible duplicate of [How to rename a file according his folder name via batch script](http://stackoverflow.com/questions/14055495/how-to-rename-a-file-according-his-folder-name-via-batch-script) – Lanting Nov 06 '14 at 09:50
  • now preparing an answer.Meanwhile you can check this http://stackoverflow.com/a/25675706/388389 – npocmaka Nov 06 '14 at 09:52
  • That colon should not be there for test case 2... – Monacraft Nov 06 '14 at 10:01
  • Solution to your question is [here](http://askubuntu.com/questions/759422/rename-files-to-their-parent-folder) – Neo Apr 20 '16 at 12:29

3 Answers3

1

This should work:

pushd C:\A_001\B_0002

for /d %%a in (*) do (
if exist "%%~a\0001.tif" ren "%%~a\0001.tif" "%%~a.tif"
)

popd

Which should do what you want. I've tested this and it works fine on my computer.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
0

i made a similar foldertree on my drive F:.

Start a commandprompt from within folder "A_001"

FOR /F "TOKENS=3,4,5* DELIMS=\" %A IN ('DIR "%CD%\*.tif" /s /b /a:-d') DO MOVE "%CD%\%A\%B\%C" "%CD%\%A_%B%~xC"

Will result in:

MOVE "F:\A_001\B_0001\123\0001.tif" "F:\A_001\B_0001_123.tif"
MOVE "F:\A_001\B_0001\ABC\0001.tif" "F:\A_001\B_0001_ABC.tif"
MOVE "F:\A_001\B_0002\123\0001.tif" "F:\A_001\B_0002_123.tif"
MOVE "F:\A_001\B_0002\ABC\0001.tif" "F:\A_001\B_0002_ABC.tif"

So you'll have all your files in the folder "A_001".

Hope this helps?

BaBa
  • 337
  • 2
  • 10
0
@echo off
for /f "delims=" %%F in ('dir /b /s /a-d "C:\A_001\0001.tif"') do (
  for %%D in ("%%~dpF.") do ren "%%F" "%%~nD%%~xF" || echo unable to rename "%%F"
)
dbenham
  • 127,446
  • 28
  • 251
  • 390