We help IT Professionals succeed at work.

Deleteing all of the directory's content

tomerlei
tomerlei asked
on
206 Views
Last Modified: 2011-09-20
i want to make a procedure that will delete an entire directory (no need to delete the directory itself but the subdirectories needs to be deleted) and do this operation from my username and password of the domain, because usally the current user wont have enough permissions to delete,it should be something like using the runAs funtion when you are logged with another user.

Comment
Watch Question

Commented:
Use FileListBox component. Specify a necessary directory to this component and remove files by using DeleteFile procedure.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i:=0 to FileListBox1.Items.Count-1 do
    DeleteFile(FileListBox1.Items[i]);
  FileListBox1.Update;
end;

CERTIFIED EXPERT

Commented:
procedure DelTree(StartDir: string);
var
  Search : TSearchRec;
begin
if Startdir[Length(Startdir)] <> '\' then
  startdir := startdir + '\';
  if FindFirst(startdir + '*.*', faAnyFile, Search) = 0 then
  repeat
    if (Search.Name[1] <> '.' ) then
      if ((Search.Attr and faDirectory) > 0) then
      begin
        RmDir(StartDir + Search.Name);    
      end else
      begin
        DeleteFile(StartDir + Search.Name);
        Application.ProcessMessages;
      end;
  until FindNext(Search) <> 0;
  FindClose(Search);
end;

Nice tidy method from Ferruccio68, not sure, but, you might to have to make some provision for removing "Hidden, Readonly and System" file attributes, should they happen to be set.

Also, in some circumstances Search.Name could be empty, you may have to validate this and use Search.FindData.cAlternateFileName (string in 8.3 filename format), OR,  Search.FindData.cFIleName (array [0..259] of char - longfilename format), at least one of these WILL have a value, so you would have to validate these too.

Beyond all that, on NT based systems, you would not be able to delete any files that happen to be "open" at the time and this would include any programs in the target folder that were currently executing.

Cheers,

David.
p.s.

You would only be able RmDir empty folders, but, you could easily modify the Ferruccio68 code to make it recursive.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
sorry, should be
procedure TForm1.Button1Click(Sender: TObject);
var
  SHFileOpStruct1: TSHFileOpStruct;
begin
  with SHFileOpStruct1 do
  begin
    Wnd := Self.Handle;
    wFunc := FO_DELETE;
    pFrom := 'd:\a';
    pTo := nil;
    fFlags := FOF_NOCONFIRMATION;
  end;
  SHFileOperation(SHFileOpStruct1);
  ForceDirectories('d:\a');
end;

Author

Commented:
it says unknown identifier TSHFileOpStruct,
maybe it's because i use delphi 5 enterprise? or i need to add something to the uses list.

Author

Commented:
it says unknown identifier TSHFileOpStruct,
maybe it's because i use delphi 5 enterprise? or i need to add something to the uses list?
Yes,

Add "ShellAPI" in your uses clause, this should be fine with Delphi5.

Cheers,

David.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.