Sorry about the 2nd half of my comment above (the 'btw' part). I looked again, and say I misread it. The original line is actually correct!
Main Topics
Browse All TopicsIn the same format, I realize that now I have names that are:
20090524091039.jpg
20090523055854.jpg
and so on..
BUT, of the two above, the first one is actually 9AM, and the second one is 5PM...I want them to be renamed on a 24-hr clock, so the first one is left as is, and the second one is renamed to "20090523175854.jpg". I have tons that are mixed like that in the same folder. Any ideas? Another script maybe?
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
On reading my previous posts, I want to be a little more clear. I am just saying that if you know for sure that hours from, for example, 01 to 06 will ALWAYS be p.m. since you know you weren't taking pictures at 2 am, then we can write something in the same script that will do the trick.
However, I don't see how this can catch all the wrongly named files. Some will have the hour as 09, and that could just as easily be 9 am as 9 pm.
Is there anything else in the filename that would give us a clue as it whether it should be a pm or am filename?
Actually, after looking at all the pictures, I realize that 95% of them are all PMs. So even if we go ahead and convert them all to PMs, then I can manually rename the AM ones, and that won't take me long.
Side Note: We actually had two cameras going...a Nikon and a Panasonic. The pics from the Nikon is the one we are fixing because it was set to a 12-hr date format and was a day later. The Panasonic was set to a 24-hr, so that was easy. I am comparing the pics we are fixing to the Panasonic, and I also just realized that the Nikon was 12 minutes behind. Can we fix that as well?
So in essence, I am asking 2 things:
1. To convert them all to PMs; and
2. To also add 12 minutes (not seconds) to the filenames
Thanks for your patience.
No problem.
Converting them all to PMs means just adding 12 hours to each number. Plus 12 minutes on top of that. It only gets a little sticky in the last 12 minutes of the day, when that would push you into the next day. Still, no problem.
Here is the full text of the new code. The only thing I am not sure will work is the line "IncMinute." It will work if your script parser has full access to the DateUtils unit. Basically, try it and if it fails, then we'll do the arithmetic the manual way. But if IncMinute works, then everything is much easier.
Let me know... :) (Signing off for tonight...)
const
HoursSpan = -1; // amount of hours to add or subtract!!
DateOutputFormat = 'yyyymmddhhnnss'; // output date format!!
HoursPerDay = 24; // do not change this!!
DaysSpan = -1;
ExtraMinutes = 12;
var
iYear, iMonth, iDay, iHour, iMin, iSec: Integer;
Date, Time, DateTime: TDateTime;
procedure AddHours(var ADateTime: TDateTime; const ANumberOfHours: Integer);
begin
ADateTime := ((ADateTime * HoursPerDay) + ANumberOfHours) / HoursPerDay;
end;
procedure AddDays(var ADateTime: TDateTime; const ANumberOfDays: Integer);
begin
AddHours(ADateTime,ANumber
end;
begin
// extract date-time variables as integers
iYear := StrToIntDef(Copy(FileName,
iMonth := StrToIntDef(Copy(FileName,
iDay := StrToIntDef(Copy(FileName,
iHour := StrToIntDef(Copy(FileName,
iMin := StrToIntDef(Copy(FileName,
iSec := StrToIntDef(Copy(FileName,
// process only if all variables are correctly converted
if (iYear >= 0) and (iMonth >= 0) and (iDay >= 0) and
(iHour >= 0) and (iMin >= 0) and (iSec >= 0) then
begin
if iHour <= 11 then iHour := iHour + 12; // make all hours into PM hours
Date := EncodeDate(iYear, iMonth, iDay);
Time := EncodeTime(iHour, iMin, iSec, 0);
DateTime := Date + Time;
// add hours or days (use minus for subtracting)
// AddHours(DateTime, HoursSpan);
IncMinute(DateTime,ExtraMi
AddDays(DateTime, DaysSpan);
// concatenate the rest of the filename and the new date
FileName := Copy(FileName, 20, Length(FileName));
FileName := FormatDateTime(DateOutputF
end
// something went wrong
else FileName := 'INVALID INPUT';
end.
great, Merjin posted this just as I was typing mine. And I mistyped the IncMinute function. It should read like Merjin's:
DateTime := IncMinute(DateTime,ExtraMi
But again, I don't know if this script has access to the full datetime functions, because if it did, there would be no need to create to the AddHours in the original script. Calling the intrinsic function "IncHour" would work better. Likewise, that's why I added the manually computed 'AddDays' instead of "IncDay"
In the loosest form, this will do the job however, take heed of what's already been said regarding the omission of an 'am' or 'pm'.
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('dir /a-d /b *.jpg') do (
set name=%%~nxa
set hour=!name:~8,2!
if !hour! lss 12 (
if !hour! lss 10 set hour=!hour:1,1!
set /a hour+=12
ren %%a !name:~0,8!!hour!!name:~10
)
)
@Merijnb - I tried to compile your script, and I get a Colon Expected, Line 1
@Joseph - I tried your script just like you had it, and it did indeed change the hours to a 24-hr format, but it also changed the day to one day prior (remember we fixed that already in another thread) and it also did not add 12 minutes to the mm.
So far, Joseph's script is super close, but again, all I want it to do is :
1. To convert them all to PMs (which Joseph's script did); and
2. To also add 12 minutes (not seconds) to the filenames
So, as an example: 20090524091039.jpg becomes 20090524212239.jpg --- where the yyyymmdd stay the same, the hh is converted to 24-hrs, and the 12 minutes are added to the mm, leaving the ss the same.
Thanks.
super786
Did you try my batch file posted earlier (see below). Copy and paste the code into Notepad and save it as RENJPG.BAT in the folder containing your .JPG files. Then run it from a command line (or DOS box).
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('dir /a-d /b *.jpg') do (
set name=%%~nxa
set hour=!name:~8,2!
if !hour! lss 12 (
if !hour! lss 10 set hour=!hour:1,1!
set /a hour+=12
ren %%a !name:~0,8!!hour!!name:~10
)
)
JosephGlosz
There was no need for that comment. My solution is batch file based as per the question tag.
Toto.. sorry, you are right. :)
The author simply wanted to get his pascal script working from within the framework of his utility. Your DOS batch file looks very impressive but you don't really know if maybe his utility has other functions, like the ability to select renaming only in specific subfolders, or to recurse subfolders, etc. So that's why I don't think a DOS batch file is all that good a solution here. Plus you have the added overhead of having to navigate to what is sometimes a deeply buried folder, then copy this batch file there, then open a DOS box there, then run this file there.....
Plus, not to be critical here of the code itself, but that sort of code looks enormously unmaintainable. Maybe that just because it's hard to understand (for me).
Super786: I'll get back to his in about an hour...
Super786:
For starters, the script works OK. The only reason it goes back a day is the constant
DaysSpan = -1;
You should make this zero. Or better, remove the 'AddDays' line entirely. I left this in there to so that you could combine this with the other script.
I assume that the "IncMinute" call worked OK? I mean, you got no syntax error when compiling or running, right?
Because, if so, I can really optimize this script.
So, for the sake of completeness, here is the full script which does NOT add or subtract any days from the output:
const
HoursSpan = -1; // amount of hours to add or subtract!!
DateOutputFormat = 'yyyymmddhhnnss'; // output date format!!
HoursPerDay = 24; // do not change this!!
ExtraMinutes = 12;
var
iYear, iMonth, iDay, iHour, iMin, iSec: Integer;
Date, Time, DateTime: TDateTime;
begin
// extract date-time variables as integers
iYear := StrToIntDef(Copy(FileName,
iMonth := StrToIntDef(Copy(FileName,
iDay := StrToIntDef(Copy(FileName,
iHour := StrToIntDef(Copy(FileName,
iMin := StrToIntDef(Copy(FileName,
iSec := StrToIntDef(Copy(FileName,
// process only if all variables are correctly converted
if (iYear >= 0) and (iMonth >= 0) and (iDay >= 0) and
(iHour >= 0) and (iMin >= 0) and (iSec >= 0) then
begin
if iHour <= 11 then iHour := iHour + 12; // make all hours into PM hours
Date := EncodeDate(iYear, iMonth, iDay);
Time := EncodeTime(iHour, iMin, iSec, 0);
DateTime := Date + Time;
IncMinute(DateTime,ExtraMi
// concatenate the rest of the filename and the new date
FileName := Copy(FileName, 20, Length(FileName));
FileName := FormatDateTime(DateOutputF
end
// something went wrong
else FileName := 'INVALID INPUT';
end.
@Merijnb - Yes.
@T0t0 - The bat file ran fine, but it changed the hours to '12' and didn't do anything with the minutes. (So 20090523023158.jpg was renamed to 20090523123158.jpg, instead of 20090523144358.jpg)
@Joseph
I can't thank you guys enough for your patience here.
Oh I see the problem. One line should be changed to:
DateTime := IncMinute(DateTime,ExtraMi
so the full script is:
const
HoursSpan = -1; // amount of hours to add or subtract!!
DateOutputFormat = 'yyyymmddhhnnss'; // output date format!!
HoursPerDay = 24; // do not change this!!
ExtraMinutes = 12;
var
iYear, iMonth, iDay, iHour, iMin, iSec: Integer;
Date, Time, DateTime: TDateTime;
begin
// extract date-time variables as integers
iYear := StrToIntDef(Copy(FileName,
iMonth := StrToIntDef(Copy(FileName,
iDay := StrToIntDef(Copy(FileName,
iHour := StrToIntDef(Copy(FileName,
iMin := StrToIntDef(Copy(FileName,
iSec := StrToIntDef(Copy(FileName,
// process only if all variables are correctly converted
if (iYear >= 0) and (iMonth >= 0) and (iDay >= 0) and
(iHour >= 0) and (iMin >= 0) and (iSec >= 0) then
begin
if iHour <= 11 then iHour := iHour + 12; // make all hours into PM hours
Date := EncodeDate(iYear, iMonth, iDay);
Time := EncodeTime(iHour, iMin, iSec, 0);
DateTime := Date + Time;
DateTime := IncMinute(DateTime,ExtraMi
// concatenate the rest of the filename and the new date
FileName := Copy(FileName, 20, Length(FileName));
FileName := FormatDateTime(DateOutputF
end
// something went wrong
else FileName := 'INVALID INPUT';
end.
Business Accounts
Answer for Membership
by: JosephGloszPosted on 2009-05-26 at 19:52:36ID: 24479601
Sorry, this'll be a problem. The output format is controlled by the variable:
9, 2), -1);
DateOutputFormat = 'yyyymmddhhnnss'; // output date format!!
this is already in 24-hr format mode. If you wanted to make it a 12 hr format, it would say:
DateOutputFormat = 'yyyymmddhhnnss am/pm'; // output date format!!
The problem is that the original filename doesn't have any kind of am/pm specifier on it. The hour part is decoded by the following line:
Hour := StrToIntDef(Copy(FileName,
This takes 2 digits from the original filename (starting at position 9) and converts it to a number. The problem is, the original filename has only (using your example) an '05' there. We have no way of knowing if that is 5 am or 5 pm, though. Judging from just the filename alone, it could just as easily have been 5 am.
Now, if you are sure that certain times HAVE to be p.m. (based on when you might be awake, etc) then I can code something that WILL change the time to the 24 format (that is, 05 to a 17) but you'd have to tell me what those hours would be.
I would put them in constants that you could change, but I still need to know something to start with...
btw, the following line is wrong:
if (iYear >= 0) and (iMonth >= 0) and (iDay >= 0) and
(iHour >= 0) and (iMin >= 0) and (iSec >= 0) then
since the hour goes from 0 to 23, and the minutes and seconds from 0 to 59, it should actually read:
if (iYear >= 0) and (iMonth >= 0) and (iDay >= 0) and
(iHour > 0) and (iMin > 0) and (iSec > 0) then
the way you currently have it, it would falsely reject some of the filename as bad.