Link to home
Start Free TrialLog in
Avatar of fibdev
fibdev

asked on

File Exists on save?

How do I showmessage to user if they are trying to save over an existing file?

<Filename> already exists, Do you wish to replace it?
          <Yes>    <No>


A full example please :)
Avatar of Jaymol
Jaymol

If FileExists(FileName) then
  If MessageDlg(FileName+' exists.  Are you sure you wish to save?',
                mtConfirmation, [mbYes, mbNo], 0)=mrYes then begin
    ...
    Do Save Stuff
    ...
  end else begin
    ...
    Do stuff if not saving
   ...
  end;
var
Ant: integer;
begin
if FileExists(Filename) then
   begin
   Ant:=MessageDlg'File '+Filename+'    already exists. Do you wish to replace it?',mtConfirmation,[mbYes,mbNo],0);
if ant=mryes then
   begin    
   //overwrite
   end;
else
   begin  
   //don't overwrite
   end;
end;

regards
rene
sorry jaymol, didn't see your comment...;-)
Avatar of fibdev

ASKER

How do I keep the savedialog open if they say no?
Avatar of simonet
A much better/easier way is to use the TSaveFileDialog component, since it can handle that sorta thing automatically.

Alex
Avatar of fibdev

ASKER

I don't have TSaveFileDilog, I don't think ...

I use Delphi 5 Standard
Avatar of fibdev

ASKER

I have TReplaceDialog but since Delphi is new to me, I don't know how to use it :(
fibdev:

"...How do I keep the savedialog open if they say no?..."

are you trying to say that if they don't want to replace they have to save the file with other name ?

then just put what jaymol or rene write in a loop sentence like :

goingOut = FALSE;
repeat

 / * here routine to ask file name */

 If FileExists(FileName) then
   If MessageDlg(FileName+' exists.  Are you sure you wish to save?',
                mtConfirmation, [mbYes, mbNo], 0)=mrYes then begin
     ...
     /* Do Save Stuff */
     goingOut = TRUE;
     ...
   end;
until goingOut;

NetoMan :}
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fibdev

ASKER

Thank you everyone for your input but this is exactly the solution i was looking for.

Neto, everyone, I will remember your input If I ever want more control than this for saving files.