#1 2015-08-28 14:52

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Copy second part of filename to the same beginning of file to its end

Hi there people!

It passed some time...but here I am big_smile

Let me explain my problem by showing some examples that are added in file table:

(C 0909-1)P1 010,015.pdf                      ------------>   (C 0909-1)P1 010,015.pdf
(C 0909-1)P2.pdf                                  ------------>   (C 0909-1)P2 010,015.pdf
UvKO(T 0906-3)P1 070,071,072,073.jpg  ------------>  UvKO(T 0906-3)P1 070,071,072,073.jpg
UvKO(T 0906-3)P2.jpg                         ------------>   UvKO(T 0906-3)P2 070,071,072,073.jpg
UvKV(C 0908-6)P1 471FL, 471,472.jpg   ------------>    UvKV(C 0908-6)P1 471FL, 471,472.jpg
UvKV(C 0908-6)P2.jpg                         ------------>   UvKV(C 0908-6)P2 471FL, 471,472.jpg

I want to add missing characters, in this case digits of corresponding filename, after P2 part. Those files are in same path, occurring various extensions. Any clues? smile


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#2 2015-08-28 23:02

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,370

Re: Copy second part of filename to the same beginning of file to its end

Love a good programming challenge! wink

The script below will compare each filename with the previous filename and will fill the missing part if both names share a significant portion of the name, as defined by minimum similarity requirements (SimilarityRatio and SimilarityMinChars constants).

Input:

(C 0909-1)P1 010,015.pdf
(C 0909-1)P2.pdf
UvKO(T 0906-3)P1 070,071,072,073.jpg
UvKO(T 0906-3)P2.jpg
UvKV(C 0908-6)P1 471FL, 471,472.jpg
UvKV(C 0908-6)P2.jpg

Output:

(C 0909-1)P1 010,015.pdf
(C 0909-1)P2 010,015.pdf
UvKO(T 0906-3)P1 070,071,072,073.jpg
UvKO(T 0906-3)P2 070,071,072,073.jpg
UvKV(C 0908-6)P1 471FL, 471,472.jpg
UvKV(C 0908-6)P2 471FL, 471,472.jpg

Script:

const
  SimilarityRatio = 0.50; // at least 50% of same characters
  SimilarityMinChars = 3; // at least 3 same characters

function CountSameCharsFromStart(A, B: WideString): Integer;
var
  I, Count: Integer;
begin
  A := WideUpperCase(A);
  B := WideUpperCase(B);
  Result := 0;
  if Length(A) < Length(B) then
    Count := Length(A)
  else
    Count := Length(B);
  for I := 1 to Count do
    if A[I] = B[I] then
      Inc(Result)
    else
      Exit;
end;

function FillFromPreviousName(const PreviousName, CurrentName: WideString): WideString;
var
  CountSameChars: Integer;
  CountSameRatio: Double;
begin
  Result := CurrentName;
  if Length(PreviousName) > 0 then
  begin
    CountSameChars := CountSameCharsFromStart(CurrentName, PreviousName);
    if CountSameChars > 0 then
    begin
      CountSameRatio := 1.0 * CountSameChars / Length(CurrentName);
      if (CountSameRatio >= SimilarityRatio) and (CountSameChars >= SimilarityMinChars) then
        Result := CurrentName + Copy(PreviousName, Length(CurrentName) + 1,
          Length(PreviousName) - Length(CurrentName));
    end;
  end;
end;

var
  PreviousName, CurrentName: WideString;

begin
  CurrentName := WideExtractBaseName(FileName);
  FileName := FillFromPreviousName(PreviousName, CurrentName) + WideExtractFileExt(FileName);
  PreviousName := CurrentName;
end.

Offline

#3 2015-08-31 07:29

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Copy second part of filename to the same beginning of file to its end

Wow you made it possible! yikes Thank you, Denis big_smile

Note: There were some undesired matches on already renamed files, but script do the job on rest of missing parts. That is because of "minimum similarity requirements".

Input:

(C 0906-1)P1 151,161-152,162.pdf
(C 0906-1)P2 151,161-152,162.pdf
(C 0906-2)P1 154.pdf
(C 0906-2)P2 154.pdf

Output:

(C 0906-1)P1 151,161-152,162.pdf
(C 0906-1)P2 151,161-152,162.pdf
(C 0906-2)P1 154,161-152,162.pdf
(C 0906-2)P2 154.pdf

Input:

(C 0906-1)P1 151,161-152,162.pdf
(C 0906-1)P2 151,161-152,162.pdf
(C 0906-2)P1 154.pdf
(C 0906-2)P2 154.pdf

Output:

(C 0906-1)P1 151,161-152,162.pdf
(C 0906-1)P2 151,161-152,162.pdf
(C 0906-2)P1 154,161-152,162.pdf
(C 0906-2)P2 154.pdf

Last edited by eR@SeR (2015-08-31 07:31)


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

Board footer

Powered by FluxBB