Board index » delphi » removing files and directories plus subs

removing files and directories plus subs

Hi

I try to remove all files within a directory, and all subdirectories
within that directory. So far, I've tried to recurse the subdirectories
and deleting the files..it works, except that it won't delete the
subdirectories..only the files.
I've also tried to use Winexec ('rmdir l:\dd\ /s',0); but it deleted
everything else on the root, and NOT that sub..
Do anyone knows how to fix this?

I use Delphi 3.0 beta.

Thanx for all help.

--
- Bj?rn Lind
bjl...@sn.no

 

Re:removing files and directories plus subs


Quote
Bj?rn Lind wrote:

> Hi

> I try to remove all files within a directory, and all subdirectories
> within that directory. So far, I've tried to recurse the subdirectories
> and deleting the files..it works, except that it won't delete the
> subdirectories..only the files.
> I've also tried to use Winexec ('rmdir l:\dd\ /s',0); but it deleted
> everything else on the root, and NOT that sub..
> Do anyone knows how to fix this?

> I use Delphi 3.0 beta.

> Thanx for all help.

> --
> - Bj?rn Lind
> bjl...@sn.no

Use Delphi's RmDir() function.

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/

Re:removing files and directories plus subs


Quote
Bj?rn Lind <bjl...@sn.no> wrote:
>Hi
>I try to remove all files within a directory, and all subdirectories
>within that directory. So far, I've tried to recurse the subdirectories
>and deleting the files..it works, except that it won't delete the
>subdirectories..only the files.
>I've also tried to use Winexec ('rmdir l:\dd\ /s',0); but it deleted
>everything else on the root, and NOT that sub..
>Do anyone knows how to fix this?
>I use Delphi 3.0 beta.
>Thanx for all help.

I have made a procedure which deletes all files with directories and
subdirectories. I think that this is what you need.

function ExtractName(dir:string):string; {Z \ -om}
begin
If dir[length(dir)]='\' then Delete(dir,length(dir),1);
result:=ExtractFileName(dir);
end;

procedure cdup(var dir:string);
var s:integer;
begin
for s:=length(dir)-1 downto 1 do
if dir[s]='\' then break;
delete(dir,s,length(dir)-s);
end;

procedure cdto(var str:string;dir:string);
begin str:=str+dir+'\'; end;

function TFormMenu.DeleteDirTree(Dir:string):boolean;
var SearchRec:array[1..10] of TSearchRec;
    r:integer;
    i:byte;
    curDir:string;
label cdupl;
begin
i:=1;
curdir:=dir+'\';
r:=FindFirst(curdir+'*.*', faAnyfile, SearchRec[i]);
if HMsg(5,r) then exit;
repeat
if (SearchRec[i].Name='.') or (SearchRec[i].Name='..') then
  begin R := FindNext(SearchRec[i]); goto cdupl; end;
{***PROCESS***}
with SearchRec[i] do
  {**DIRECTORY**}
  if SearchRec[i].Attr=faDirectory then
      begin
      cdto(curdir,Name);
      inc(i);
      R:=FindFirst(curdir+'*.*', faAnyfile, SearchRec[i]);
      HMsg(5,R);
      end
  else
  {**FILE**}
  DeleteFile(curDir+Name);
R := FindNext(SearchRec[i]);
{***CD.. za node-e***}
cdupl:
if r<>0 then
  begin
  FindClose(SearchRec[i]);
  RemoveDir(curdir);
  dec(i);
  cdup(curdir);
  if i>0 then R:=FindNext(SearchRec[i]);
  if (r<>0) and (i<>0) then goto cdupl;
  end;
until (r<>0) and (i=0);
result:=true;
removedir(dir);
end;

--------------------------------------------------------
 Name   : Mitja Perko
 E-Mail : Mitja.Pe...@guest.arnes.si
 WWW    : http://www2.arnes.si/guest/osljbk1/index.html
--------------------------------------------------------

Other Threads