Solved

"globally" closing all open file handles?

Posted on 2002-06-05
7
409 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 260
Performance of SQL statement 37 112
FMX and jaudiotracker playing memory stream 29 79
Multi-layered image in FireMonkey 9 37
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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