Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a File Name !! 2

Status
Not open for further replies.

Creeden

Programmer
May 9, 2002
16
0
0
CA
I have a program that uses a file called VE3CRM.dat... I would like the user to be able to make a new file called..
Some other name. My Procedure is this code.
Code:
procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(AddrFile, 'VE3CRM.DAT');
  if FileExists('VE3CRM.DAT') then
    begin
        Reset(AddrFile);
      if (FileSize(AddrFile) > 0) then
      begin
        Seek(AddrFile, 0);
        ReadRec;
        NewRec := False;
      end;
    end
  else
    begin
      Rewrite(AddrFile);
      NewRec := True;
    end;
  btnNew.Enabled := True;
  btnInsert.Enabled := False;
  btnUpdate.Caption := '&Update';
end;

How can I code a choice for the user to enter a new file name in a popup box or something.
Thanks [pipe]
Bob
 
Bob,

How about something like this?

Code:
var
   strOldName,
   strNewName : String;
begin

   strOldName := 'c:\xyzzy.txt';
   strNewName := strOldName;
   if inputQuery( 'Rename',
                  format( 'Enter a new name for %s:', [strOldName] ),
                  strNewName ) then
      renameFile( strOldName, strNewName );

You'll need Dialogs and SysUtils in an appropriate Uses block and you'll probably want to add error-checking.

Hope this helps...

-- Lance

 
Hi Lance.
That works great except I have to maintain the old file also. This code changes the name. It is a logbook program for "Ham Radio". I wanted to be able to, let's say, have one File as the "VE3CRM.dat" and then if I wanted to I could make a new file something like "Contest1.dat" and still have the VE3CRM.dat to use as my regular logbook.
Thanks for your time.
Bob :)
 
If you want to copy a file all you need is:

var
lvOverwrite: Boolean;
begin
lvOverwrite := False;
CopyFile(pchar('c:\a.txt'),pchar('d:\a.txt'),lvOverright);
end;

where

c:\a.txt is the source file
d:\a.txt is the destination
lvOverright is whether the destination should be overwritten if the file already exists

 
Hello mikeEd, and others who are trying to help me.
What I need is to be able to create a complete new Logbook. Lets call it the VE3CRM.dat. On some date in the future there is a contest starting and I want a new Logbook to store my contacts, OPQ.dat. I need a way to change between these Logbooks at anytime they are needed. It could turn out that after a year or so I might have Logbook1....Logbook20. The program runs beautiful except not being able to create new .dat files.
Code:
AssignFile(AddrFile, 'VE3CRM.DAT'); [b](need something here to[/b]
  if FileExists('VE3CRM.DAT') then  [b](make the change !!!)[/b]
    begin
        Reset(AddrFile);
      if (FileSize(AddrFile) > 0) then
      begin
        Seek(AddrFile, 0);
        ReadRec;
        NewRec := False;
      end;
    end
  else
    begin
      Rewrite(AddrFile);
      NewRec := True;
    end;
  btnNew.Enabled := True;
  btnInsert.Enabled := False;
  btnUpdate.Caption := '&Update';
end;
I can't find any information about this, anywhere.
Maybe a popup with a editbox in it when the form loads. Could be a button on the form. I dunno. :-(
Thanks, [pipe]
Bob
 
Hmm...Looks like the bold tags did not work.
 
Still not sure this is entirely what you are after, but anyway: (this will switch to using whatever text file is specified in the input box)

private
{ Private declarations }
TheFileName : String;
AddrFile : TextFile;

procedure TForm1.FormCreate(Sender: TObject);
begin
TheFileName := '';
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseFile(AddrFile);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
OldFileName : String;
begin
OldFileName := TheFileName;
InputQuery('Filename','Please enter name of logbook',TheFileName);
if TheFileName <> OldFileName then
begin
if OldFileName <> '' then
CloseFile(AddrFile);
AssignFile(AddrFile, TheFileName);
end;
if FileExists(TheFileName) then
begin
Reset(AddrFile);
if (FileSize(AddrFile) > 0) then
begin
Seek(AddrFile, 0);
ReadRec;
NewRec := False;
end;
end
else
begin
Rewrite(AddrFile);
NewRec := True;
end;
btnNew.Enabled := True;
btnInsert.Enabled := False;
btnUpdate.Caption := '&Update';
end;
 
Hello Robertio.
Thank you very much for all the help. Your code works great except for the file close statement. It causes an error 103, file not open. For now I commented it out. Otherwise it is super good. I just started Delphi a week ago and find it really good. I only have the Personal Edition, but it does what I want.
Code:
begin
    if OldFileName <> '' then
      {CloseFile(AddrFile);}  <-----Here
    AssignFile(AddrFile, TheFileName);
     Reset(AddrFile);
  end;
I would also like to thank all the others that answered my post and made suggestions.
Thanks
Bob :)
 
The procedure Reset (Addrfile) opens the file for read or write. To intercept any errors use the IOResult function

example

{$I-} //disable input/output checking
Reset (AddrFile);
{$I+} //enable input/output checking

if IOResult = 0 then //no errors found
begin
do your stuff
end

else


The meaning of the error codes for IOresult are:

2 File not found
3 Path not found
5 Access denied
32 Sharing violation

100 End of File
101 Disk Full
102 File variable not assigned
103 File not open
104 File not open for input
105 File not open for output
106 Error in formatted input
107 File already open

Tip
To get help on a function, procedure or reserved word, type in IOResult, highlight it and press F1
Delphi will show the help on that topic

Regards



S. van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top