filename collision

Posted on 2004-04-05
Last Modified: 2010-05-19

  is there an algorithm that will prevent a rename collision. or should i say if i have a file name
  file1.txt and file2.txt   and i will rename file2.txt to file1.txt it will not prompt for filename exists
  but instead it will rename it to file1a.txt and so on and so forth?

Question by:aerokevs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 17

Accepted Solution

mokule earned 125 total points
ID: 10756443
// procedure renames file OldName to NewName
// if file NewName exists the name is expanded with _NNN
// starting from _001
// until free number is found

procedure MyRenameFile(OldName,NewName: string);
  TempName,Ext : string;
  P: integer;
  Cnt: integer;
  if FileExists(NewName) then
    Ext := ExtractFileExt(NewName);
    P := Pos(Ext,NewName);
    TempName := Copy(NewName,1,P-1);
    Cnt := 1;
      NewName := Format('%s_%3.3d%s',[TempName,Cnt,Ext]);
    until not FileExists(NewName);

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question