Forum > General

increment filename if exists (google chrome style)

(1/3) > >>

Ericktux:
hello friends, good day to all, a question, if I have the text:

happy_monchito

How can I make it increment as long as the same text already exists, resulting in:

happy_monchito
happy_monchito (1)
happy_monchito (2)
happy_monchito (3)

the same way google chrome does when downloading a file that already exists.

Zvoni:
Check if FileExists (*hint*)
If Yes, FindAllFiles (*hint*) with that Pattern (into a StringList)
Sort the StringList
If StringList.Count>1 Then Read the last Entry of the List
    Check the "number" inside the paranthesis at the end of that entry
    Add plus 1
    Construct the "new" Filename
Else Set Filename to FileName (1)

Warfley:
This would require a custom compare function (as in string compare 2>10), which makes this actually quite complicated.

I would recommend the much easier solution, to simply count up, check if file with the current number does exist, if it doesn't use the number to create the file

Zvoni:

--- Quote from: Warfley on May 16, 2022, 09:32:10 am ---I would recommend the much easier solution, to simply count up, check if file with the current number does exist, if it doesn't use the number to create the file

--- End quote ---
Warfly,
he would still have to check first, if the Filename without number already exists.
Then Count up, and what you said

Lansdowne:

--- Quote from: Warfley on May 16, 2022, 09:32:10 am ---This would require a custom compare function (as in string compare 2>10), which makes this actually quite complicated.

I would recommend the much easier solution, to simply count up, check if file with the current number does exist, if it doesn't use the number to create the file

--- End quote ---

I am not sure counting is necessary, nor is it foolproof.
Suppose the following files already exist:
greatest_hits
greatest_hits (1986)
greatest_hits (1987)
greatest_hits (1988)
Then the counting method would append the number 4 to the name which makes no sense.
What we would expect is either 1 (simplest non-existent name) or 1989 (so the newest name is always the highest number).

Simplest non-existent name:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function GetUniqueName(ThisDir, ThisName :string) :string);const FmtStr='%s (%d)';var sufx: integer;   TryThis: string;  //i omitted this at firstbeginif trim(ThisName)='' then begin   // something to do if an empty string is provided ; for example:  ThisName:='unnamed';end;if not FileExists(ThisDir+ThisName) then begin   result:=ThisName;   exitend;sufx:=0;repeat   inc(sufx);   TryThis:=Format(FmtStr,[ThisName,sufx]);until not FileExists(ThisDir+TryThis);result:=TryThisend; 

Navigation

[0] Message Index

[#] Next page

Go to full version