Sebastion
asked on
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
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
ASKER
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(sS earch), 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.
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(sS
The parameters that I'm sending across are:
delete_all_directorie('D:\
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.
ASKER
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_'))
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, replace this line
h1:= findfirstfile(pwidechar(sS earch), wfd);
with this one
h1:= findfirstfile(pchar(sSearc h), wfd);
thats it :) and instead of your_procedure_to_delete_t he_directo ry(...) you could use atul_parmars function of course, if you want
h1:= findfirstfile(pwidechar(sS
with this one
h1:= findfirstfile(pchar(sSearc
thats it :) and instead of your_procedure_to_delete_t
ASKER
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(s Base);" 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(sSearc h), 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?
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(s
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(sSearc
Of course, Sebastion. Just DelTree('c:\temp\hello_*') ; is enough and there are no complications just careful operation.
Try it.
Try it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
var
h1: thandle;
wfd: win32_find_data;
begin
result:= 0;
h1:= findfirstfile(pwidechar(sS
if h1= INVALID_HANDLE_VALUE then exit;
sBase:= includetrailingbackslash(s
repeat
if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY<>
if (strpas(wfd.cFileName)<> '.') and (strpas(wfd.cFileName)<> '..') then begin
call_your_procedure_to_del
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_t
begin
...
end;
...you could call the above like this:
delete_all_directories('C:
this way all folders with a name beginning with "Backup" are removed out of C:\Windows\System32
not tested, but should work