Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to increment filename

I'm trying to make a function that can increment a filename. If last char of the string is a number then increment it. If last char is a letter then add _1 or _2 or _3(increment this also). I have to be sure the filename is unique but i cannot use datetime inside filename because all the filenames must be <32 chars without extension.

EX: Apple_99.txt =>Apple_100
Ex: Apple_173 => Apple_174
EX: This_is_my_first_text.txt => This_is_my_first_text_1.txt
Ex: This_is_my_first_text_9.txt => This_is_my_first_text_10.txt

I need to use this in order to rename a file an then upload it to a ftp server. I've found a function that can do something like this but it only works if the filename contains only uppercase.How can I modify this function in order to access lowercase an uppercase string?

Here is the function:

    function IncStr(Str: String; Amount: Integer; Index: Integer = -1): String;
    const
     MIN_VAL = 65; // 'A'
     MAX_VAL = 90; // 'Z'
    var
     Digit, ToAdd, ToCarry: Integer;
    begin

     if (Index = 0) and (Amount > 0) then
      begin
       Result := Char(MIN_VAL + Amount - 1) + Str;
       Exit;
      end;

     if Index = -1 then Index := Length(Str);

     ToCarry := 0;

     Digit := Ord(Str[Index]);

     while not (Digit in [MIN_VAL..MAX_VAL]) do
      begin
       Dec(Index);
       Digit := Ord(Str[Index]);
      end;

     ToAdd := Digit + Amount;

     while (ToAdd > MAX_VAL) do
      begin
       Dec(ToAdd, 26);
       Inc(ToCarry);
      end;

     Result := Str;
     Result[Index] := Char(ToAdd);

     if (ToCarry > 0) then
      Result := IncStr(Result, ToCarry, Index - 1);

    end;


    procedure TForm1.Button1Click(Sender: TObject);
    var
     S: String;   // holds string to increment
     C: Integer;  // amount to increment by
    begin
     // make sure that Edit1 starts with a valid character
     // i.e. 'A' to 'Z'
     S := Edit1.Text;
     C := StrtoIntDef(Edit2.Text, 0);
     // test it, place result in Edit3
     Edit3.Text := IncStr(S, C);
     {
       Example data:

         Edit1 := AAZ
         Edit2 := 2
       = Edit3 := ABB

         Edit1 := BZY
         Edit2 := 3
       = Edit3 := CAB

         Edit1 := ZZZ
         Edit2 := 1
       = Edit3 := AAAA

         Edit1 := AA-AC
         Edit2 := 3
       = Edit3 := AA-AF

         Edit1 := AA/Z
         Edit2 := 5
       = Edit3 := AB/E

       ... etc

       Here's one to try too :-)
         Edit1 := ZZZ
         Edit2 := 264172
     }
    end;

Thank you!

like image 882
user2858981 Avatar asked Mar 03 '26 11:03

user2858981


1 Answers

Like so many programming problems, the key is to break the problem down into small pieces. First of all, let's write a function to decode the original file name into its constituent parts:

procedure DecodeFileName(const Input: string; out Stem, Ext: string; out Number: Integer);
var
  P: Integer;
begin
  Ext := TPath.GetExtension(Input);
  Stem := TPath.GetFileNameWithoutExtension(Input);
  Number := 0;

  P := Stem.LastIndexOf('_');
  if P = -1 then begin
    exit;
  end;

  if TryStrToInt(Stem.Substring(P+1), Number) then begin
    Stem := Stem.Substring(0, P);
  end;
end;

The following demonstrates how this works:

DecodeFileName('test.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

DecodeFileName('test_dd.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

DecodeFileName('test_23.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

The output is:

test, 0, .txt
test_dd, 0, .txt
test, 23, .txt

So now you can make a new filename like this:

function IncrementedFileName(const FileName: string): string;
var
  Stem, Ext: string;
  Number: Integer;
begin
  DecodeFileName(FileName, Stem, Ext, Number);
  Result := Format('%s_%d%s', [Stem, Number+1, Ext]);
end;

And then we can see how that performs:

Writeln(IncrementedFileName('test.txt'));
Writeln(IncrementedFileName('test_dd.txt'));
Writeln(IncrementedFileName('test_23.txt'));
Writeln(IncrementedFileName('test_28'));

The output is:

test_1.txt
test_dd_1.txt
test_24.txt
test_29

If you don't have access to the string helper methods then you can code it like this:

procedure DecodeFileName(const Input: string; out Stem, Ext: string; out Number: Integer);
var
  P: Integer;
begin
  Ext := TPath.GetExtension(Input);
  Stem := TPath.GetFileNameWithoutExtension(Input);
  Number := 0;

  P := LastDelimiter('_', Stem);
  if P = 0 then begin
    exit;
  end;

  if TryStrToInt(Copy(Stem, P+1, MaxInt), Number) then begin
    Stem := Copy(Stem, 1, P-1);
  end;
end;

I have not executed this final function, so do not be surprised if it has errors.

like image 61
David Heffernan Avatar answered Mar 06 '26 03:03

David Heffernan