Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Check ik file exist

Dear experts,

I have this to load and save the 3 listviews to file.

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

But when I start my application the first time the files doesn't exits yet.
so, I get 3 times a error message. How can I prevent this.
That the programm first look if the fileexist otherwist create it.

P
Avatar of bokist
bokist
Flag of Hungary image

Try to check this way :

if FileExists(File_Name)  then  DeleteFile(File_Name);
Avatar of Peter Kiers

ASKER

It has to be like this:

If file does not exits in the application-folder
then create one.

P.
If I understand you correctly,  this will do it :

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');
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   if FileExists('Glucose.sav')  then   DeleteFile('Glucose.sav');
   SaveListViewToFile(lstvGlucose, 'Glucose.sav');
   if FileExists('Bolus.sav')  then   DeleteFile('Bolus.sav');
   SaveListViewToFile(lstvBolus, 'Bolus.sav');
   if FileExists('FoodDB.sav')  then   DeleteFile('FoodDB.sav');
   SaveListViewToFile(lstvFoodDB, 'FoodDB.sav');
I found out myself:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  PgCtrlMain.ActivePage := tbshGlucose;
  if FileExists(ExtractFilePath(Application.ExeName) + 'Glucose.sav') then
  LoadListViewToFile(lstvGlucose, 'Glucose.sav');
  if FileExists(ExtractFilePath(Application.ExeName) + 'Bolus.sav') then
  LoadListViewToFile(lstvBolus, 'Bolus.sav');
  if FileExists(ExtractFilePath(Application.ExeName) + 'FoodDB.sav') then
  LoadListViewToFile(lstvFoodDB, 'FoodDB.sav');
end;
ASKER CERTIFIED SOLUTION
Avatar of bokist
bokist
Flag of Hungary image

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