I am using delphi 7.
What I am doing in this code is reading a txt file that has 4 lines of information. With those 4 lines of information i am then creating another text file (with the extension of .index). I need to make sure that the txt file exists before attempting to create the index file, but using the try...except...end; does not work when i have two in a row. it always creates the index file without all the information. How can i go about using the error handling of try except but have the second one conditional on the first?
What I am doing in this code is reading a txt file that has 4 lines of information. With those 4 lines of information i am then creating another text file (with the extension of .index). I need to make sure that the txt file exists before attempting to create the index file, but using the try...except...end; does not work when i have two in a row. it always creates the index file without all the information. How can i go about using the error handling of try except but have the second one conditional on the first?
Code:
procedure TForm1.readtxtfile(fn:String);
procedure GetValue(var value:string);
var
position : integer;
begin
if AnsiContainsText(value, '=') then { Does the line contain an equals sign }
begin
position:=AnsiPos('=', value); { Find the location of the equals sign }
value:=TRIM(MidStr(value,position+1,length(value))); { Get Value after the equals sign }
end;
end;
var
mytxt, myindex: TextFile;
AppName, DateSub, Company, PolType : String;
sLOB : String;
i:integer;
begin
// if fn <> '' then begin
try
{ Reading the Text File }
AssignFile(mytxt, srcDir+'\'+fn+'.txt'); { Open Text File }
FileMode := fmOpenRead; { Open the file in read only mode }
Reset(mytxt); { Open file for reading }
Readln(mytxt, AppName); { Read Applicant's Name Line }
GetValue(AppName); { Get Just Applicant's Name }
Readln(mytxt, DateSub); { Read Date Submitted Line }
GetValue(DateSub); { Get Just Date Submitted }
Readln(mytxt, Company); { Read Company Line }
GetValue(Company); { Get Just Company }
Readln(mytxt, PolType); { Read Policy Type Line }
GetValue(PolType); { Get Just LOB }
cdsFilesAppName.AsString := AppName; { Add Applicant's Name to Client Dataset Table }
cdsFilesDateSub.AsString := DateSub; { Add Date Submitted to Client Dataset Table }
cdsFilesCompany.AsString := Company; { Add Company to Client Dataset Table }
cdsFilesPolType.AsString := PolType; { Add Policy Type to Client Dataset Table }
CloseFile(mytxt); { Close file }
except
{ Email because there is no corresponding txt file }
myMsg.Add('There is no corresponding txt file for' + fn + '.pdf');
SendEmail(strIniFile, myMsg);
end;
try
{ Writing/Building the index file }
AssignFile(myindex, dstDir+'\'+fn+'.index'); { Create Index File }
Rewrite(myindex); { Open file for writing }
Write(myindex,Item1+Sepr); { Write Values from ini file }
Write(myindex,Item2+Sepr); { Write Values from ini file }
Write(myindex,Item3+Sepr); { Write Values from ini file }
Write(myindex,Item4+Sepr); { Write Values from ini file }
Write(myindex,Item5+Sepr); { Write Values from ini file }
Write(myindex,dstDir+'\'+fn+'.pdf'+Sepr); { Write Destination Directory and PDF file name }
Write(myindex,Item7+Sepr); { Write Values from ini file }
for i := 1 to CLHM do { Determine if the Policy Type is Commercial or Personal }
begin
if PolType = CLLOB[i] then begin { Compare the Policy type to the list of Commercial LOBs retrieved from the ini file }
sLOB := 'CL';
break;
end
else if PolType = PLLOB[i] then begin { Compare the Policy type to the list of Personal LOBs retrieved from the ini file }
sLOB := 'PL';
break;
end;
end;
Write(myindex,sLOB+Sepr); { Write CL or PL }
Write(myindex,AppName+Sepr); { Write Applicant's name }
Write(myindex,PolType+Sepr); { Write Policy type }
Write(myindex,Company+Sepr); { Write Company }
Write(myindex,Item12+Sepr); { Write Values from ini file }
Write(myindex,Item13+Sepr); { Write Values from ini file }
Write(myindex,Item14+Sepr); { Write Values from ini file }
Write(myindex,Item15+Sepr); { Write Values from ini file }
Write(myindex,Item16+Sepr); { Write Values from ini file }
Write(myindex,DateSub+Sepr); { Write Date Submitted }
Write(myindex,Item18); { Write Values from ini file }
CloseFile(myindex); { Close file }
except
{ Email because unable to create index file }
myMsg.Add('Unable to create index file');
SendEmail(strIniFile, myMsg);
end;
// end;
end; {}