#1 2011-10-05 13:57

colonelmustard
Member
Registered: 2011-10-05
Posts: 2

Extracting date from file name

Love the software for basic use..but lost in the advanced use of it. I need some help with filenames that are a bit out of the norm.

I have close to 1000 pdf files that I need to rename based on an obscure date within them. ie:

fd1152007.pdf  need  fd_Nov_5_2007.pdf (I know this could actually be 1_15_2007 but I will have to fix those manually)
fd122007.pdf  need  fd_Jan_22_2007.pdf (ditto)

In the future the company is going to use two character dates and a delimiter but that won't help me today! But something to help me rename 9 months of the year correctly will help right now. All of the PDFs were created on the same day so the file created date is of no use.

Once all of the file names are changed, is there a way to make the date shown in the new file name, as the date created property with this software?

Any help would be appreciated.

I think I am in for a lonnng couple of days.  roll

Offline

#2 2011-10-05 15:31

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

Re: Extracting date from file name

I've sometimes had seen files named like that and since then I've been thinking on trying to do a script that would help me "decode" them

So I've picked your question and I've done a quick script.

Maybe there are something left, and I'm sorry that I completely changed the output format. But the way it's done now it's easier to correct later.

So take a look at it and give your feedback for changes. (You don't need to rename them, preview is enough.)

var
    Parts: TstringsArray;
    Date, Day, Month, Evaluation: WideString;
    
begin
    Parts := SubMatchesRegEx(FileName, '^(.*?)(\d+)(\d{4})(.*?)$', False);
    
    Date := Parts[1];

    case length(Date) of
        1: Evaluation := 'ERROR';
        
        2: begin
            Month := Date[1];
            Day := Date[2];
            Evaluation := 'OK';
        end;
        
        3: begin
            if (Date[3]='0') then // For days like 10, 20, 30.
            begin
                Month := Date[1];
                Day := Date[2] + Date[3];
                Evaluation := 'OK';
            end
            else
            begin
                if (StrToInt(Date[2])<3) then // Months 10, 11, 12.
                begin
                    Month := Date[1] + Date[2];
                    Day := Date[3];
                    
                    if (Month='10') then
                        Evaluation := 'OK'
                    else
                        Evaluation := 'DOUBT';
                end
                else
                begin
                    Month := Date[1];
                    Day := Date[2] + Date[3];
                    Evaluation := 'OK';
                end;
            end;
        end;
        
        4: begin
            Month := Date[1] + Date[2];
            Day := Date[3] + Date[4];
            Evaluation := 'OK';
        end;
    else
        Evaluation := 'ERROR';
    end;
    
    while (length(Month)<2) do Month := '0' + Month;
    while (length(Day)<2) do Day := '0' + Day;
    
    FileName := '{' + Evaluation + '} ' + Parts[0] + ' [' + Parts[2] + '-' + Month + '-' + Day + ']' + Parts[3];
end.

(We will speak about changing the file date after having them well renamed.)


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

Offline

#3 2011-10-09 04:43

colonelmustard
Member
Registered: 2011-10-05
Posts: 2

Re: Extracting date from file name

Thanks so much for the wonderful script. There were a couple of dates that did not format right but I changed those manually. You are a real timesaver! Is changing the modified date to this date featured now correctly in the name difficult?

Last edited by colonelmustard (2011-10-09 04:44)

Offline

#4 2011-10-09 16:55

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

Re: Extracting date from file name

Is changing the modified date to this date featured now correctly in the name difficult?

Not difficult but a little dangerous. The following script is applied directly on preview (not on rename as usual), and there's no undo possibility after.

So, please proceed with caution.

var
  DateMod: TDateTime;
  DateParts: TStringsArray;
  Year, Month, Day: Word;
  Initialized, Accept: Boolean;

begin
  if not Initialized then
  begin
    Initialized := True;
    Accept := WideDialogYesNo(
      'WARNING: This script will apply changes right now. ' +
      'There''s no way to preview the result and it can not be undone.' + #10#10 + 
      'Do you still want to coninue?'
    );
  end;
  
  if not Accept then Exit;

  DateParts := SubmatchesRegEx(FileName, '(\d{4})-(\d{1,2})-(\d{1,2})', False);

  if (length(DateParts)=3) then
  begin
    Year  := StrToInt(DateParts[0]);
    Month := StrToInt(DateParts[1]);
    Day   := StrToInt(DateParts[2]);
  
    DateMod := EncodeDate(Year, Month, Day);
  
    if SetFileTimeModified(FilePath, DateMod) then
      FileName := ':: OK >> Date changed succesfully ::'
    else
      FileName := ':: FAIL >> Date not changed ::';
  end
  else
  begin
    FileName := ':: FAIL >> No date found ::';
  end;
end.

If the program has helped you, please, take a look (if you haven't already) at the donations and licence page, maybe you could also help the author wink.


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

Offline

Board footer

Powered by FluxBB