We help IT Professionals succeed at work.

Save files to a specified folder

Peter Kiers
Peter Kiers used Ask the Experts™
on
Hi,

I have this for saving the data of several listviews to file.

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveListViewToFile(lstvGlucose, 'Glucose.sav');
  SaveListViewToFile(lstvBolus, 'Bolus.sav');
  SaveListViewToFile(lstvFoodDB, 'FoodDB.sav');
  SaveListViewToFile(lstvOwnDB, 'OwnDB.sav');
end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  if FileExists('Glucose.sav') then LoadListViewToFile(lstvGlucose, 'Glucose.sav');
  if FileExists('Bolus.sav') then LoadListViewToFile(lstvBolus, 'Bolus.sav');
  if FileExists('FoodDB.sav') then LoadListViewToFile(lstvFoodDB, 'FoodDB.sav');
  if FileExists('OwnDB.sav') then LoadListViewToFile(lstvOwnDB, 'OwnDB.sav');
end;
(*---------------------------------------------------*)

These files (if not exists) will be created and save to the application-folder.
But now I have made a folder called Data in the application-folder and I
want the programm to save (or create the files) in the folder.
I quess I have to change the code. Can someone help me?

P.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
Just change the paths to include Data\, i.e


procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveListViewToFile(lstvGlucose, 'Data\Glucose.sav');
  SaveListViewToFile(lstvBolus, 'Data\Bolus.sav');
  SaveListViewToFile(lstvFoodDB, 'Data\FoodDB.sav');
  SaveListViewToFile(lstvOwnDB, 'Data\OwnDB.sav');
end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  if FileExists('Data\Glucose.sav') then LoadListViewToFile(lstvGlucose, 'Data\Glucose.sav');
  if FileExists('Data\Bolus.sav') then LoadListViewToFile(lstvBolus, 'Data\Bolus.sav');
  if FileExists('Data\FoodDB.sav') then LoadListViewToFile(lstvFoodDB, 'Data\FoodDB.sav');
  if FileExists('Data\OwnDB.sav') then LoadListViewToFile(lstvOwnDB, 'Data\OwnDB.sav');
end;
(*---------------------------------------------------*)
Peter KiersOperator

Author

Commented:
Thanks. 500 points are comming to you...

PK
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
Peter, just so that you learn how to do that the proper way and start to think more about re-usability and easier maintenance :
- when you do something more than once in pretty much the same way, create a procedure. always.
your code will be even tighter.
in this case, you already have LoadListViewToFile, so what is the point of testing if file exists outside ?
same for the 'Data\', why not have it added in LoadListViewToFile ?
- that done, you can use default parameters in these functions to give them a standard, default behaviour (save in current dir) or a specialized (save in specific dir)

const
 DefaultDataSubDir='Data\';
// DefaultDataSubDir='.'; // This was your old behaviour

function GetFullDir(Dir:String):String;
begin
 if Dir='' Then Dir:='.';
 Dir:=ExpandFileName(Dir);
 Result:=IncludeTrailingBackslash(Dir);
end;

procedure SaveListViewToFile(lv:TListView;FileName:String;Dir:String=DefaultDataSubDir);
begin
 FileName:=GetFullDir(Dir)+FileName;
//.. do what you already did before
end;

procedure LoadListViewToFile(lv:TListView;FileName:String;Dir:String=DefaultDataSubDir);
begin
 FileName:=GetFullDir(Dir)+FileName;
 if Not FileExists(FileName) Then Exit;
//.. do what you already did before
end;


procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveListViewToFile(lstvGlucose, 'Glucose.sav');
  SaveListViewToFile(lstvBolus, 'Bolus.sav');
  SaveListViewToFile(lstvFoodDB, 'FoodDB.sav', '..\OtherRelativeDataDir');
  SaveListViewToFile(lstvOwnDB, 'OwnDB.sav''\OneRootToCurrentDriveDataDir');
end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  LoadListViewToFile(lstvGlucose, 'Glucose.sav');
  LoadListViewToFile(lstvBolus, 'Bolus.sav');
  LoadListViewToFile(lstvFoodDB, 'FoodDB.sav', '..\OtherRelativeDataDir');
  LoadListViewToFile(lstvOwnDB, 'OwnDB.sav', '\OneRootToCurrentDriveDataDir');
end;

Open in new window