24-Hr TimeStamp vs 12-Hr timestamp

In 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.
LVL 1
super786Asked:
Who is Participating?
 
JosephGloszConnect With a Mentor Commented:
Oh I see the problem.  One line should be changed to:

    DateTime := IncMinute(DateTime,ExtraMinutes);

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, 1, 4), -1);
  iMonth := StrToIntDef(Copy(FileName, 5, 2), -1);
  iDay   := StrToIntDef(Copy(FileName, 7, 2), -1);
  iHour  := StrToIntDef(Copy(FileName, 9, 2), -1);
  iMin   := StrToIntDef(Copy(FileName, 11, 2), -1);
  iSec   := StrToIntDef(Copy(FileName, 13, 2), -1);
 
  // 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,ExtraMinutes);
 
    // concatenate the rest of the filename and the new date
    FileName := Copy(FileName, 20, Length(FileName));
    FileName := FormatDateTime(DateOutputFormat, DateTime) + FileName;
  end
 
  // something went wrong
  else FileName := 'INVALID INPUT';
end.
0
 
JosephGloszCommented:
Sorry, this'll be a problem. The output format is controlled by the variable:

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, 9, 2), -1);

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.

0
 
JosephGloszCommented:
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!

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JosephGloszCommented:
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?
0
 
super786Author Commented:
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.
0
 
MerijnBSr. Software EngineerCommented:
See example routine, in short it does:
- check if given filename is correct timestamp
- check if it's a AM time, if so add 12 hours (make it PM)
- add 12 seconds

Are you able to make the rest (some mechanism which will feed all filenames in here and rename the files)?

function TForm1.FixFilename(Filename: string): string;
var Timestamp: TDateTime;
    FileExt: string;
begin
 // save original file extension
 FileExt := ExtractFileExt(Filename);
 
 // sanity check
 if Length(ChangeFileExt(Filename, '')) <> 14 then
  result := Filename
 else
 begin
  try
   Timestamp := EncodeDateTime(StrToInt(Copy(Filename, 1, 4)),
                               StrToInt(Copy(Filename, 5, 2)),
                               StrToInt(Copy(Filename, 7, 2)),
                               StrToInt(Copy(Filename, 9, 2)),
                               StrToInt(Copy(Filename, 11, 2)),
                               StrToInt(Copy(Filename, 13, 2)), 00);
 
   // check if it's not PM yet, if not, convert it to PM
   if HourOfTheDay(Timestamp) < 13 then
    Timestamp := IncHour(Timestamp, 12);
 
   // add 12 minutes
   Timestamp := IncMinute(Timestamp, 12);
 
   // generate the result from our current timestamp with original file extension
   result := FormatDatetime('yyyymmddhhnnss', Timestamp) + FileExt;
  except
   // something wrong in provided filename, just return the input
   result := Filename;
  end;
 end;
end;

Open in new window

0
 
JosephGloszCommented:
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,ANumberOfDays*HoursPerDay);
end;
 
begin
  // extract date-time variables as integers
  iYear  := StrToIntDef(Copy(FileName, 1, 4), -1);
  iMonth := StrToIntDef(Copy(FileName, 5, 2), -1);
  iDay   := StrToIntDef(Copy(FileName, 7, 2), -1);
  iHour  := StrToIntDef(Copy(FileName, 9, 2), -1);
  iMin   := StrToIntDef(Copy(FileName, 11, 2), -1);
  iSec   := StrToIntDef(Copy(FileName, 13, 2), -1);
 
  // 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,ExtraMinutes);
    AddDays(DateTime, DaysSpan);
 
    // concatenate the rest of the filename and the new date
    FileName := Copy(FileName, 20, Length(FileName));
    FileName := FormatDateTime(DateOutputFormat, DateTime) + FileName;
  end
 
  // something went wrong
  else FileName := 'INVALID INPUT';
end.
0
 
JosephGloszCommented:
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,ExtraMinutes);

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"

0
 
MerijnBSr. Software EngineerCommented:
Oops, we're in the FreePascal zone :p

Dunno, can't test in FreePascal atm.
0
 
t0t0Commented:
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!
   )
)
0
 
JosephGloszCommented:
Toto, this isn't kansas...
0
 
super786Author Commented:
Guys... @ work now. Will go home in the evening and give it a shot. I will keep everyone in the know.

Thanks.
0
 
super786Author Commented:
@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.
0
 
MerijnBSr. Software EngineerCommented:
> @Merijnb - I tried to compile your script, and I get a Colon Expected, Line 1

Line 1, as in function TForm1.FixFilename(Filename: string): string; ??
0
 
t0t0Commented:
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.
0
 
JosephGloszCommented:
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...  
0
 
JosephGloszCommented:
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, 1, 4), -1);
  iMonth := StrToIntDef(Copy(FileName, 5, 2), -1);
  iDay   := StrToIntDef(Copy(FileName, 7, 2), -1);
  iHour  := StrToIntDef(Copy(FileName, 9, 2), -1);
  iMin   := StrToIntDef(Copy(FileName, 11, 2), -1);
  iSec   := StrToIntDef(Copy(FileName, 13, 2), -1);
 
  // 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,ExtraMinutes);
 
    // concatenate the rest of the filename and the new date
    FileName := Copy(FileName, 20, Length(FileName));
    FileName := FormatDateTime(DateOutputFormat, DateTime) + FileName;
  end
 
  // something went wrong
  else FileName := 'INVALID INPUT';
end.






 
0
 
super786Author Commented:
@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 ran the last script that you posted, and it changes the hh to a 24-hr format without changing the date, just like I requested. However the minutes still stay the same. (So 20090523023158.jpg gets renamed to 20090523143158.jpg, instead of 20090523144358.jpg)

I can't thank you guys enough for your patience here.
0
 
super786Author Commented:
@Joseph - It worked!!! Thanks a TON for the time and effort in helping me with this one.
0
 
t0t0Commented:
My mistake, the code should have been:

@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 (
      set /a hour=!hour!+12
      ren %%a !name:~0,8!!hour!!name:~10!
   )
)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.