D2: RemoveDir causes Sharing violation in NT4.0

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
zaibatzukaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pjdbCommented:
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
zaibatzukaAuthor Commented:
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
pjdbCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

pjdbCommented:
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
zaibatzukaAuthor Commented:
Hi zaibatzuka,

Have you tried the function RemoveDirectory (Win32 reference)?

Regards, Zif.
0
ZifNabCommented:
Hi,

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

Regards, Zif.
0
ZifNabCommented:
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
zaibatzukaAuthor Commented:
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
ZifNabCommented:
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
KECommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zaibatzukaAuthor Commented:
How do your app. make use of the directory ?
Do you open files, or is it a database via BDE ?
0
KECommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.