Jonathan Lyon
Jonathan Lyon

Reputation: 3952

create windows 8 batch file to copy, rename and save files from sub directories recursively

I'm trying to copy thousands of image files and rename them with the name of the folder they are in. The file structure is:-

C:\pictures\kitcam\1\master_01.jpg
C:\pictures\kitcam\1\master_02.jpg
C:\pictures\kitcam\2\master_01.jpg
C:\pictures\kitcam\3\master_01.jpg
C:\pictures\kitcam\3001\master_01.jpg

I would like to create a new directory C:\pictures\kitcam\all and copy and rename the files above to the following naming convention:-

c:\pictures\kitcam\all\[directoryname]_filename]
(pad directory name to 4 digits so that the director name 1 becomes 0001 etc)

for example:-

C:\pictures\kitcam\all\0001_master_01.jpg 

Jonathan

Upvotes: 1

Views: 3214

Answers (1)

Endoro
Endoro

Reputation: 37589

@ECHO OFF &SETLOCAL
SET "startfolder=C:\pictures\kitcam"
SET "targetfolder=C:\pictures\kitcam\all"

FOR /r "%startfolder%" %%a IN (*.jpg) DO (
    SET "fname=%%~nxa"
    SET "fpath=%%~fa"
    FOR /f "delims=" %%b IN ("%%~dpa.") DO SET "nname=000%%~nxb"
    SETLOCAL ENABLEDELAYEDEXPANSION
    ECHO MOVE "!fpath!" "%targetfolder%\!nname:~-4!_!fname!"
    ENDLOCAL
)

Look at the output and remove the word echo before move if it looks good.

Upvotes: 3

Related Questions