delphi - Delete Directory with non empty subdirectory and files -
how delete 1 directory having files , non empty sub directory.
have tried shfileoperation function. has compatibility issue in windows 7.
have tried ifileoperation interface. not compatible in windows xp. have tried following codes suggested david heffernan :
procedure tmainform.bitbtn01click(sender: tobject); var fileanddirectoryexist: tsearchrec; resourcesavingpath : string; begin resourcesavingpath := (getwindir) + 'web\wallpaper\'; if findfirst(resourcesavingpath + '\*', faanyfile, fileanddirectoryexist) = 0 try repeat if (fileanddirectoryexist.name <> '.') , (fileanddirectoryexist.name <> '..') if (fileanddirectoryexist.attr , fadirectory) <> 0 //it's directory, empty clearfolder(resourcesavingpath +'\' + fileanddirectoryexist.name, mask, recursive) else //it's file, delete deletefile(resourcesavingpath + '\' + fileanddirectoryexist.name); until findnext(fileanddirectoryexist) <> 0; //now directory empty, can delete removedir(resourcesavingpath); findclose(fileanddirectoryexist); end; end;
but not compiled mentioning error undeclared identifier @ clearfolder, mask , recursive. requirement "if sub folder exist under wallpaper folder deleted". same sub folder may contain number of non empty sub folder or files.
well, starters, shfileoperation
has no compatibility issues on windows 7 or windows 8. yes, recommended use ifileoperation
instead. if want support older operating systems xp, can , should call shfileoperation
. works , continue work. it's pefectly fine use on windows 7 , windows 8 , i'll eat hat if it's ever removed windows. microsoft go extraordinary lengths maintain backwards compatibility. so, shfileoperation
best option in view.
your findfirst
based approach fails because need put in separate function in order allow recursion. , code posted in other answer incomplete. here complete version:
procedure deletedirectory(const name: string); var f: tsearchrec; begin if findfirst(name + '\*', faanyfile, f) = 0 begin try repeat if (f.attr , fadirectory <> 0) begin if (f.name <> '.') , (f.name <> '..') begin deletedirectory(name + '\' + f.name); end; end else begin deletefile(name + '\' + f.name); end; until findnext(f) <> 0; findclose(f); end; removedir(name); end; end;
this deletes directory , contents. you'd want walk top level directory , call function each subdirectory found.
Comments
Post a Comment