?
Solved

D2: RemoveDir causes Sharing violation in NT4.0

Posted on 1998-03-19
12
Medium Priority
?
511 Views
Last Modified: 2012-06-21
To remove a back-up structure consisting of a directory tree with a number of files I use a recursive DelTree function. The function works in Win 3.11 using RmDir (Delphi 1), but if I recompile it with Delphi 2 under NT4.0 (no compile errors or warnings) using RemoveDir or RmDir I get I/O error 32 when the function tries to remove an empty directory. All files is removed, but I end up with the directory structure intact. The back-up structure is created by zipping up a stored structure. Does anyone know why I get I/O error 32 and how I could get rid of it?

FUNCTION:
procedure DelTree (const path : String);
  var
     d:        TSearchRec;
     tmppath, tmp:  string;
     found:    integer;
  begin
    if path[length(path)] <> '\' then
       tmppath := path + '\'
    else
       tmppath := path;

    found := FindFirst(tmppath + '*.*',faDirectory,d);
    while  found = 0 do begin
       if (d.Name <>'.') and (d.Name<>'..') and (d.Attr = faDirectory) then
         DelTree(tmppath + d.name);
       found := FindNext(d);
      end;

    found := FindFirst(tmppath + '*.*',faAnyFile,d);
    while  found = 0 do begin
      if (d.Name <>'.') and (d.Name<>'..') then
        DeleteFile(tmppath + d.name);
      found := FindNext(d);
      end;
    tmp := Copy(tmppath, 1, Length(tmppath)-1); { Removes '\' }
    FindClose( d );
    RemoveDir(tmp);
  end;

Regards,

Zaibatsuka
0
Comment
Question by:zaibatzuka
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 1

Expert Comment

by:pjdb
ID: 1360607
There is a mistake in the code... try this one :

procedure DelTree (const path : String);
var
  d:        TSearchRec;
  tmppath, tmp:  string;
  found:    integer;
begin
  if path[length(path)] <> '\' then
    tmppath := path + '\'
  else
    tmppath := path;

  found := FindFirst(tmppath + '*.*',faDirectory,d);
  while  found = 0 do
  begin
    if (d.Name <>'.') and (d.Name<>'..') and (d.Attr = faDirectory) then
      DelTree(tmppath + d.name);
    found := FindNext(d);
  end;
  FindClose( d );

  found := FindFirst(tmppath + '*.*',faAnyFile,d);
  while  found = 0 do
  begin
    if (d.Name <>'.') and (d.Name<>'..') then
      DeleteFile(tmppath + d.name);
    found := FindNext(d);
  end;
  FindClose( d );
  tmp := Copy(tmppath, 1, Length(tmppath)-1); { Removes '\' }
  RemoveDir(tmp);
end;
0
 

Author Comment

by:zaibatzuka
ID: 1360608
It does not remove the root directory and strangely enough the structure is still visible in the File Explorer "All Folders" section!! ( Not in the "Contents of" section, though ).
0
 
LVL 1

Expert Comment

by:pjdb
ID: 1360609
In the explorer, you need to update (F5) to make sure that it display the disk content.
It seems strange that it does not remove the root directory (the last line of the procedure should do it)

JDB
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:pjdb
ID: 1360610
I have tried the function 5-6 times. It does not remove the root directory due to I/O error 32 (RmDir). The other problem was solved after rebooting the computer.
0
 

Author Comment

by:zaibatzuka
ID: 1360611
Hi zaibatzuka,

Have you tried the function RemoveDirectory (Win32 reference)?

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360612
Hi,

Mmmm, I was thinking.... Is it possible that you want to delete the directory on wich you stand?

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360613
Thanks, pjdb and ZifNab,
The root directory still wont go, but it's probably something in my program that makes the sharing violation. As such I rest my case (at least publicly) for now.
0
 

Author Comment

by:zaibatzuka
ID: 1360614
Yo zaibatzuka,

I used only your source code and still it gave that problem. It has something to do with the  DelTree (But I don't know what this function does). So try to change this and have a look.
If you only use RemoveDir('c:\directory') it works perfectly!!! So my guess is that it is DelTree....

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360615
I have a complete customized set of file-management routines, and I have no problems with the below routines:

//----------------------------------------------------------------------------
Function fcDeleteFile( Destination: String ) : Boolean;
begin
  Result := DeleteFile( PChar(Destination) );
end;

//----------------------------------------------------------------------------
Function fcRemoveDir( Destination: String; RemoveSub: Boolean ) : Boolean;
var
  SearchRec : TSearchRec;
  x : Integer;
begin
  If RemoveSub then begin
    x := FindFirst(Destination+'\*.*', faAnyFile, SearchRec);
    while x = 0 do begin
      with SearchRec do begin
        If (Name<>'.') AND (Name<>'..') then begin
          Case (Attr AND faDirectory)<>0 of
            True : fcRemoveDir( Destination+'\'+Name, RemoveSub );
            False : fcDeleteFile( Destination+'\'+Name );
          end;
        end;
      end;
      x := FindNext(SearchRec);
    end;
    FindClose(SearchRec);
  end;
  Result := RemoveDirectory( PChar(Destination) );
end;

See if it works - if not, make sure that your not deleting a directory that the OS or another application controls.

0
 
LVL 3

Accepted Solution

by:
KE earned 600 total points
ID: 1360616
The answer is still, for me, that something in my application does something to this directory. It wont go until I close the app and then removes it via the file browser. I have tried all advices you have given me and it does not help. Sorry for such a bad end to a fairly good question! pjdb, I am sorry I did not grade your answer correctly.

Thanks and regards,

Zaibatsuka
0
 

Author Comment

by:zaibatzuka
ID: 1360617
How do your app. make use of the directory ?
Do you open files, or is it a database via BDE ?
0
 
LVL 3

Expert Comment

by:KE
ID: 1360618
I use the directory as a work directory with a back-up structure, error files and other files used by the app. The app. creates the error file in the directory and a DOS application uses files in the work directory for downloading of code to an external non-PC computer ( real-time embedded system ). It is not the back-up program ( a 16-bit Windows program) that is the problem. Either the DOS application or the creation of the error file is doing this, whatever "this" it is.

Regards,

Zaibatsuka
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

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…
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…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

621 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