Solved

filename collision

Posted on 2004-04-05
1
232 Views
Last Modified: 2010-05-19
Hi,

  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?


0
Comment
Question by:aerokevs
1 Comment
 
LVL 17

Accepted Solution

by:
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);
var
  TempName,Ext : string;
  P: integer;
  Cnt: integer;
begin
  if FileExists(NewName) then
    begin
    Ext := ExtractFileExt(NewName);
    P := Pos(Ext,NewName);
    TempName := Copy(NewName,1,P-1);
    Cnt := 1;
    repeat
      begin
      NewName := Format('%s_%3.3d%s',[TempName,Cnt,Ext]);
      Inc(Cnt);
      end;
    until not FileExists(NewName);
    end;
  RenameFile(OldName,NewName);
end;
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now