• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Searching for and Deleting multiple directories

Hello,

I want to know if there's a way to search for and delete multiple directories (assuming they all have a similiar name).  I've already handled deleting a directory if it's got files/folders in.

For example, suppose that there was 5 directories, with the following name formats:

backup_1
backup_2
backup_3
backup_4
backup_5


If they were files (and if it was command prompt), you probably could type 'del backup*' to delete them all, but as far as I know there is no such method for directories.

Basically, the system would need to search for all folders that start with the name backup_ (including the underscore) and delete them.

Any help would be appreciated

Thanks
0
Sebastion
Asked:
Sebastion
  • 4
  • 3
  • 2
2 Solutions
 
AmigoJackCommented:
procedure delete_all_directories(sBase, sSearch: string);
var
  h1: thandle;
  wfd: win32_find_data;
begin
  result:= 0;
  h1:= findfirstfile(pwidechar(sSearch), wfd);
  if h1= INVALID_HANDLE_VALUE then exit;

  sBase:= includetrailingbackslash(sBase);
  repeat
    if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY<> 0 then
    if (strpas(wfd.cFileName)<> '.') and (strpas(wfd.cFileName)<> '..') then begin
      call_your_procedure_to_delete_the_directory(sBase+ strpas(wfd.cFileName));
    end;
  until not findnextfile(h1, wfd);
  windows.findclose(h1);
end;


assuming your procedure to delete a folder is named like this:...

procedure your_procedure_to_delete_the_directory(sFoldername: string);
begin
...
end;

...you could call the above like this:

delete_all_directories('C:\Windows\System32', 'Backup*');

this way all folders with a name beginning with "Backup" are removed out of C:\Windows\System32


not tested, but should work
0
 
SebastionAuthor Commented:
Hey,

I receive the following error when I try to compile the code:

Incompatible types: 'WideChar' and 'Char'


The line that it's pointing to is "h1:= findfirstfile(pwidechar(sSearch), wfd);" from the delete_all_directories procedure.

The parameters that I'm sending across are:

delete_all_directorie('D:\temp', 'Hello_')

The directories in this case that need to be deleted are "Hello_1", "Hello_2", etc.

Also, result was an undeclared identifier, which I assume because we are not returning a value, so I've just commented it out for now.
0
 
SebastionAuthor Commented:
edit, disregard that typo up there, the procedure name is spelt correctly in the code (ie it reads delete_all_directories('D:\temp', 'Hello_'), not delete_all_directorie('D:\temp', 'Hello_'))
0
Get your problem seen by more experts

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

 
atul_parmarCommented:
Hi, I don't know how your deletion routine looks like but sure that you would not need it. Just paste the following function and say DelTree('c:\backup*');

uses ShellAPI;
Function DelTree(DirName : string): Boolean;
var
  SHFileOpStruct : TSHFileOpStruct;
  DirBuf : array [0..255] of char;
begin
  try
   Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
   FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
   StrPCopy(DirBuf, DirName) ;
   with SHFileOpStruct do begin
    Wnd := 0;
    pFrom := @DirBuf;
    wFunc := FO_DELETE;
    fFlags := FOF_ALLOWUNDO;
    fFlags := fFlags or FOF_NOCONFIRMATION;
    fFlags := fFlags or FOF_SILENT;
   end;
    Result := (SHFileOperation(SHFileOpStruct) = 0) ;
   except
    Result := False;
  end;
end;

Be careful, this routine will not ask for confirmation.
0
 
AmigoJackCommented:
sorry, replace this line

  h1:= findfirstfile(pwidechar(sSearch), wfd);

with this one

  h1:= findfirstfile(pchar(sSearch), wfd);

thats it :) and instead of  your_procedure_to_delete_the_directory(...) you could use atul_parmars function of course, if you want
0
 
SebastionAuthor Commented:
atul_parmar, I actually already use DelTree to force delete the directories, regardless of what is in there.  If I could use the same function to delete all directories starting with the same name it would be great.  At the moment, I simply call the function as (for example) DelTree('c:\temp\hello_1'); which deletes a single directory.  Is it enough to simply use DelTree('c:\temp\hello_*'); without any complications?


AmigoJack, the procedure still doesn't appear to be working correctly.  It compiles now, but it doesn't remove any of the directories listed in the path passed to it.  I stepped through the procedure and up until "sBase:= includetrailingbackslash(sBase);" the variable "sBase" has a value (eg 'D:\temp') but after that line it doesn't.  

I'm not sure if that's how the procedure is supposed to work.  Likewise, I'm having difficulties getting my head around why we can perform a "h1:= findfirstfile(pchar(sSearch), wfd);" without first specifying where we want the system to find the files to begin with (ie, the path listed in the variable sBase).  Could you clear some of this up for me?
0
 
atul_parmarCommented:
Of course, Sebastion. Just DelTree('c:\temp\hello_*'); is enough and there are no complications just careful operation.

Try it.
0
 
AmigoJackCommented:
yes, you are right - sorry. ok, this isnt tested again, but it should work now. dont know why "includetrailingbackslash()" messes up at your side, so you better declare it yourself:

 procedure delete_all_directories(sBase, sSearch: string);
var
  h1: thandle;
  wfd: win32_find_data;
begin
  result:= 0;
  if length(sBase)= 0 then sBase:= '\' else
  if sBase[length(sBase)]<> '\' then sBase:= sBase+ '\';
  h1:= findfirstfile(pchar(sBase+ sSearch), wfd);
  if h1= INVALID_HANDLE_VALUE then exit;

  repeat
    if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY<> 0 then
    if (strpas(wfd.cFileName)<> '.') and (strpas(wfd.cFileName)<> '..') then begin
      call_your_procedure_to_delete_the_directory(sBase+ strpas(wfd.cFileName));
    end;
  until not findnextfile(h1, wfd);
  windows.findclose(h1);
end;


hope it works now :)
0
 
SebastionAuthor Commented:
atul_parmar, thanks that's what I needed to know.  I had previously tried the astericks


AmigoJack, I really appreciate your effort with regards to this, but since I'm already using DelTree it would be considerably easier to re-use it to do what I need to achieve (something I previously didn't know worked), rather than implement another procedure.  I haven't had time to test the new version of your procedure, but I expect it to work based on what I can see.


I've increased the points and split it between you two, giving most of it to atul_parmar since it fit perfectly into what I had.

Thanks again you two.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now