?
Solved

"globally" closing all open file handles?

Posted on 2002-06-05
7
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

Accepted Solution

by:
Slick812 earned 200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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