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
Solved

D2: RemoveDir causes Sharing violation in NT4.0

Posted on 1998-03-19
12
503 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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