Martin Ward
Martin Ward

Reputation: 29

bulk rename files within a folder to parent folder name

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

Upvotes: 1

Views: 1472

Answers (3)

dbenham
dbenham

Reputation: 130819

@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"
)

Upvotes: 0

BaBa
BaBa

Reputation: 337

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?

Upvotes: 0

Monacraft
Monacraft

Reputation: 6630

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.

Upvotes: 1

Related Questions