Avatar of Rob Rudloff
Rob Rudloff
Flag for United States of America asked on

Batch rename files sequentially and move

Hi.  I need help writing a batch file ..

I have randomly-named .jpg files in a folder (it's always C:\TEMP).  
I would like to rename these files to a argument that I pass, but add a sequential number (preferably sorted by file datetime).  
Then, the files would be moved to a folder that is passed in another argument (folder exists, and it's name is based on the current month, so it will always be in format "F:\PICS\yyyy-mm".

SO, I'd call something like:   RENAME.BAT "MyPicture"
and the files would be renamed and moved:
"C:\TEMP\bbb.jpg"    2017/04/03 13:45:45  --- >   "F:\PICS\2017-04\MyPicture-01.jpg"
"C:\TEMP\aaa.jpg"    2017/04/03 13:46:01  --- >   "F:\PICS\2017-04\MyPicture-02.jpg"
"C:\TEMP\ggg.jpg"    2017/04/03 13:47:01  --- >   "F:\PICS\2017-04\MyPicture-03.jpg"
"C:\TEMP\ddd.jpg"    2017/04/03 13:47:02  --- >   "F:\PICS\2017-04\MyPicture-04.jpg"

Open in new window

I was able to create the path based on the date, with something like:
set yyyy=%date:~-4%
set mm=%date:~4,2%
set FolderName= F:\PICS\%yyyy%-%mm%

Open in new window

But, I got stuck on the sequential renaming bit,  Any ideas?
-- thanks!
Windows BatchWindows OS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rob Rudloff

ASKER
Ah, excellent, Bill!
I modified it for a quick test, see below.  
I put a hyphen before the !Count! so it named files like "MyPicture-1.jpg"
So, is there a way to format the  !Count!  to force a two digit number, like "MyPicture-01.jpg" ?
Can I return the value of Count back from the batch file?
Thanks!
Rob
@echo off
setlocal EnableDelayedExpansion

set BaseDir=D:\TESTDATA\PicQueue
set BaseName=%~1

set yyyy=%date:~-4%
set mm=%date:~4,2%
set DestDir=D:\TESTDATA\%yyyy%-%mm%

set Count=0
for /f "tokens=*" %%A in ('dir /b /od "%BaseDir%\*.jpg"') do (
  set /a Count += 1
  move "%BaseDir%\%%~A" "%DestDir%\%BaseName%-!Count!.jpg"
)

Open in new window

SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rob Rudloff

ASKER
That seems to do everything I need!  (You are wise in the ways of "batch")

Now, I put this in a file called MovePics.bat  and I call it like:
    movepics.bat  MyPics
It renames the 10 files in that folder properly (like MyPics-01.jpg, and so on)  but I don't see a return value of 10.  
Is there a trick to calling it from the command line to see the return code?

Thanks!
Rob
Bill Prew

In the calling BAT script, the return code is passed back in the %ERRORLEVEL% variable, just display or use it after the call to this bath file.

~bp
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rob Rudloff

ASKER
Ok, I''ll give the  %ERRORLEVEL%  thing a try.
Thanks for your help on all this!
Rob
Bill Prew

Welcome, thanks for the feedback.

~bp