#1 2012-06-20 20:58

edneri
Member
Registered: 2012-06-20
Posts: 1

Modify date and time stamp between file name

Hey Guys, need your help to "re arrange" the name of a file, I have a bunch of files with the format: "MM-DD-YYYY HH-MM-SS AM.XXX" and I want to re arrenge it into "YYYY-MM-YY HH.MM.SS.XXX" with the AM or PM converted into the 24hr format, i.e. 10 PM to 22 Hrs.
Part of the issue is that months 1 to 9 are not listed as 01 or 09 but only as 1 or 9, same problem with the time.

Some examples:

1-1-2012 9-1-10 PM.XXX to 2012-01-01 21.01.10.XXX
10-5-2012 8-10-10 AM.XXX to 2012-10-05 08.10.10.XXX

Thanks in advance for all your help!

Offline

#2 2012-06-20 23:48

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Modify date and time stamp between file name

Hi, for that purpose somwhere or another you where going to need Regular Expresions, so for simplicty I built this

Test it specially on 12:00 PM and 12:00 AM to see if you get what you want and day, month order

var
  Parts: TStringsArray;
  Temp: WideString;
  I: Integer;
  
begin
  Parts := SubMatchesRegEx(FileName, '^(.*?)(\d{1,2})-(\d{1,2})-(\d{4}) (\d{1,2})-(\d{1,2})-(\d{1,2}) (AM|PM)(.*?)$', false);
  
  if (length(Parts) <> 9) then
    Exit;
  
  if (WideSameText(Parts[7], 'PM')) then
  begin
    Parts[4] := IntToStr(StrToInt(Parts[4]) + 12);
    if Parts[4] = '24' then Parts[4] := '0';
  end;

  for I:=1 to 6 do
  begin
    while (length(Parts[i]) < 2) do
    begin
      Temp := '0' + Parts[i];
      Parts[i] := Temp;
    end;
  end;
  
  FileName := Parts[0] +
    Parts[3] + '-' + Parts[1] + '-' + Parts[2] + ' ' + 
    Parts[4] + '.' + Parts[5] + '.' + Parts[6] +
    Parts[8];
end.

wink


If this software has helped you, consider getting your pro version. :)

Offline

#3 2012-06-21 15:06

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Modify date and time stamp between file name

Good work SafetyCar.

I had an another idea:

FROM:
MM-DD-YYYY HH-MM-SS AM.XXX
1-1-2012 12-3-10 Pm.XXX
10-5-2012 8-10-10 aM.XXX
TO:
YYYY-MM-YY HH.MM.SS.XXX
padding single digits with zero to double digits,
with the PM converted into the 24hr format,
2012-01-01 00.03.10.XXX
2012-05-10 08.10.10.XXX

USE e.g.:

var
  SpaceParts, DashParts: TStringsArray;
  temp,s: WideString;
  i: Integer;

begin

  //Get the base name w/o extension:
  temp := WideExtractBaseName(FileName);
  //======================================
  //Split the basename "MM-DD-YYYY HH-MM-SS AM" at the space:
  SpaceParts := WideSplitString(temp, ' ');
  //If the current file don't match the requirements, skip it:
  if (length(SpaceParts) < 3) then exit;
  //======================================
  //the date "MM-DD-YYYY": 
  // split into parts again at the hypen,
  // pad each part with zero's and rearrange the parts:
  DashParts := WideSplitString(SpaceParts[0], '-');
  //If the current file don't match the requirements, skip it:
  if (length(DashParts) < 3) then exit;
  for i := 0 To 1 do
   begin
     if length(DashParts[i]) < 2 Then
     begin
       s := DashParts[i];
            DashParts[i] := '0' + s;
     end;
   end;
  temp := DashParts[2] +'-'+ DashParts[1] +'-'+ DashParts[0];
  //======================================
  //the time "HH-MM-SS": 
  // split into parts at the hyphen,
  // pad each part with zero's and rearrange the parts:
  DashParts := WideSplitString(SpaceParts[1], '-');
  if (length(DashParts) < 3) then exit;
  for i := 0 To 2 do
   begin
    if length(DashParts[i]) < 2 Then 
    begin
         s := DashParts[i];
              DashParts[i] := '0' + s;
    end;
   end;
  //======================================
  //the PM:
  // If the third part of the "space-parts" is smtg like "PM",
  // add '12' to any PM-hour; exchange already '12' by '00': 
  if WideUpperCase(SpaceParts[2]) = 'PM' Then
  begin
       i := StrToInt(DashParts[0]);
    if i < 12 Then   
              DashParts[0] := IntToStr(i + 12);
    if i = 12 Then   
              DashParts[0] := '00';
  end;
  temp := temp +' '+DashParts[0]+'.'+DashParts[1]+'.'+DashParts[2];
  //======================================
 //Compose the new file name:
 FilePath := '';
 FileName := temp + WideExtractFileExt(FileName);
end.

HTH? big_smile


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

Board footer

Powered by FluxBB