Solved

D2: RemoveDir causes Sharing violation in NT4.0

Posted on 1998-03-19
12
504 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
[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
  • 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
Independent Software Vendors: 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 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 200 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

Technology Partners: 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!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

733 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