Solved

"globally" closing all open file handles?

Posted on 2002-06-05
7
405 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

17 Experts available now in Live!

Get 1:1 Help Now