MS DOS Batch File to Rename & Move Files from nested file structure

Hello,

I would like to modify the following code to recursively move files from c:\pictures to here c:\new pictures and rename them pre-fixed with their sub folder name, can anyone help?

Thanks

@echo off
 
setlocal
 
pushd "c:\pictures"
 
for /f "tokens=*" %%a in ('dir /b /a-d /s *.jpg') do copy "%%a" "c:\new pictures"
 
popd

Open in new window

SickBoy23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveGTRCommented:
You could try this. I've disabled the copy command so that it is echoed out. To do the actual copy you would remove the echo command from the 2nd for statement. I've commented the line for you:
@echo off
 
setlocal
 
pushd "c:\" 
 
call :PROCESS "pictures"
 
popd
 
goto :EOF
 
:PROCESS
 
pushd "%~1"
 
for /f "tokens=*" %%a in ('dir /b /ad 2^>NUL') do call :PROCESS "%%a"
 
REM ** Remove echo from this line to actually do the copy
for /f "tokens=*" %%a in ('dir /b /a-d *.jpg 2^>NUL') do echo copy "%%a" "c:\new pictures\%~1_%%a"
 
popd
 
goto :EOF

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SickBoy23Author Commented:

Steve the code works great!! Appreciated!

Thanks
0
SickBoy23Author Commented:

Steve, if I want to change the paths e.g. move & rename the contents of C:\Temp\NewFolder\Pics to here C:\Temp\NewFolder, how would I do this?

Thanks
0
SickBoy23Author Commented:

Ignore the above figured it out, I was doing something stupid!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.