Solved

"globally" closing all open file handles?

Posted on 2002-06-05
7
407 Views
Last Modified: 2010-04-04
Hi...
  Is there a statment that will do a "global" close of ALL open file handles in one shot?
  In other words, if I have several files open via this way:

assignfile(infile1, c:\tempfile1.txt');
reset(infile1);
assignfile(infile2, c:\tempfile2.txt');
reset(infile2);
assignfile(infile3, c:\tempfile3.txt');
reset(infile3);
assignfile(infile4, c:\tempfile4.txt');
reset(infile4);

Is there a quick way to "close" them all at once, instead of having to individually close each one.

Thanks
   Shawn
0
Comment
Question by:aztec
7 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7058692
If you cannot place the infile1 .. N in an array then switch over to TFileStreams. A simple for loop will do the work then.
0
 

Author Comment

by:aztec
ID: 7058929
but the file handles  are not named infile1...N - that was only an example. they are completely different names.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 7060834
you could use an array, just the same


var
  FileArray: Array of File;
  i: Integer;
  Str1: String;
begin
SetLength(FileArray,5);
assignfile(FileArray[0], c:\sempfile.txt');
reset(FileArray[0]);
Readln(FileArray[0],Str1);
assignfile(FileArray[1], c:\hempfile.txt');
reset(FileArray[1]);
assignfile(FileArray[2], c:\dempfile.txt');
reset(FileArray[2]);
assignfile(FileArray[3], c:\mempfile.txt');
reset(FileArray[3]);
assignfile(FileArray[4], c:\rempfile.txt');
reset(FileArray[4]);


for i := 0 to 4 do
  CloseFile(FileArray[i]);
end;

in your code use the FileArray[1] instead of a variable name
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7060840
I think he is looking for a way to look for globally open file handles... (application-wide) so that when the application terminates (perhaps even if abnormally?) it will still release all the open (locked?) files.

:)
0
 

Author Comment

by:aztec
ID: 7062603
Dragonslayer - Exactly!!

Is there something that can do this?
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 50 total points
ID: 7063258
I'm not to sure what's needed here but I don't know of any thing to get Files or TextFiles, except by their variable names or pointers to the variable names.  I used the method for TypeCasting the FileArray[0] with TTextRec(FileArray[0]).mode to determine if the Text file is open or closed. If you use an Array of File instead of of an Array of TextFile you can TypeCast with TFileRec( ). I use arrays alot with file searches, like the example below. In the FormDestroy procedure is my version of a Make sure all files are closed.


{Memo1 gets the Text output}
private
    { Private declarations }
    FileArray: Array of TextFile;


procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
{this is for an abnormal program end to be sure all files are closed,
the mode check will prevent IO exceptions}
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  CloseFile(FileArray[i]);
end;

procedure TForm1.but_OpenFClick(Sender: TObject);
  var
  FS, i: Integer;
  SRec: TSearchRec;
  Dir, S: String;
begin
{this button click searches a Folder and gets the text files}
Dir := 'C:\Stuff\';
FS := FindFirst( Dir + '*.txt' , faHidden , SRec ) ;
if FS = 0 then
  begin
  try
    SetLength(FileArray,1);
    AssignFile(FileArray[0], Dir + SRec.Name);
    Reset(FileArray[0]);
    Read(FileArray[0], S);
    Label1.Caption := SRec.Name;
    while FindNext(SRec) = 0 do
      begin
      SetLength(FileArray,Length(FileArray)+1);
      AssignFile(FileArray[High(FileArray)], Dir + SRec.Name);
      end;
    finally
    FindClose(SRec) ;
    end;
  end;

Label1.Caption := IntToStr(Length(FileArray));
for i := 0 to High(FileArray) do
  begin
  Memo1.Text := Memo1.Text+#13#10+TTextRec(FileArray[i]).Name;
  {I added the Name to show you how to get the file name after AssignFile}
  if (TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode then
  Reset(FileArray[i]);
  ReadLn(FileArray[i], S);
  Memo1.Text := Memo1.Text+#13#10+S;
  if i = 0 then
  CloseFile(FileArray[i]);
  {only one file is closed to test the mode check}
  end;
end;

procedure TForm1.but_CloseFClick(Sender: TObject);
var
i: Integer;
begin
{this button click just does the close file and then checks to be sure the file is closed}
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  begin
  CloseFile(FileArray[i]);
  Memo1.Text := Memo1.Text+#13#10+'Closed file '+IntToStr(i);
  end;
Memo1.Text := Memo1.Text+#13#10+'NEW';
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  begin
  Memo1.Text := Memo1.Text+#13#10+'Open '+IntToStr(i);
  end
else
  begin
  Memo1.Text := Memo1.Text+#13#10+'Closed '+IntToStr(i);
  end;
end;
0
 

Author Comment

by:aztec
ID: 7084847
That TTextRec.mode is just what I need. Thanks Slick812.

Cheers
   Shawn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now