Solved

filename collision

Posted on 2004-04-05
1
231 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

16 Experts available now in Live!

Get 1:1 Help Now