Solved

D2: RemoveDir causes Sharing violation in NT4.0

Posted on 1998-03-19
12
501 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:pjdb
Comment Utility
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
Comment Utility
Hi zaibatzuka,

Have you tried the function RemoveDirectory (Win32 reference)?

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Hi,

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

Regards, Zif.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now