Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Deleting a Directory

sudendrasudendra Member Posts: 17
How can I delete a directory in Delphi ?
I have a directory which has some files.I delete all the files first.
Then I try to delete the directory.But the directory dosent get deleted.

I have tried all the availabel remove and delete commands for Directory.

Please let me know if u have any other commands.

Comments

  • zibadianzibadian Member Posts: 6,349
    : How can I delete a directory in Delphi ?
    : I have a directory which has some files.I delete all the files first.
    : Then I try to delete the directory.But the directory dosent get deleted.
    :
    : I have tried all the availabel remove and delete commands for Directory.
    :
    : Please let me know if u have any other commands.
    :
    I take it you tried RemoveDir(). How did you delete the files? Did you use the FindXXXX() routines to scan for all the files in the directory? Are there any other directories in the directory?
  • sudendrasudendra Member Posts: 17
    : : How can I delete a directory in Delphi ?
    : : I have a directory which has some files.I delete all the files first.
    : : Then I try to delete the directory.But the directory dosent get deleted.
    : :
    : : I have tried all the availabel remove and delete commands for Directory.
    : :
    : : Please let me know if u have any other commands.
    : :
    : I take it you tried RemoveDir(). How did you delete the files? Did you use the FindXXXX() routines to scan for all the files in the directory? Are there any other directories in the directory?
    :
    ****************************************************************
    Hi Zibadan,

    I knew u would reply.
    Well I have used many thing .. all the commands and even created a batch file.
    First thing is I am clearing all the files.
    While debugging as well I have made sure that the directory is empty before I execute the delete command for directory.
    Now I am using a batch file.In it I use the 'rmdir ' + Dirname.
    I create after cleaning up the directory.And then execute it.
    But it dosent delete the directory.But when I run the same batch file
    manually then it deletes the directory.Funny isnt it ?????

    Though when I seperately create a small small prog then it deletes the dir.
    *********SAMPLE PROG******************
    If DirectoryExists ( ExtractFilePath ( Application.ExeName ) + UPGRADEFOL ) then begin
    st_Folder := ExtractFilePath ( Application.ExeName ) + UPGRADEFOL;
    DeleteFile ( st_folder + SAMPLEPROGBAT );
    End;
    batchfile := TStringList.Create;
    batchname := ExtractFilePath ( Application.ExeName ) + TEMPBAT;
    with batchfile do begin
    try
    Add('rmdir "' + st_Folder + '"');
    Add('del "' + batchname + '"');
    SaveToFile(batchname);
    finally
    batchfile.Free;
    end;
    end;
    WinExec(PChar(batchname), SW_HIDE);
    close;
    ***********END OF SAMPLE PROG***************

    FUNNY ISNT IT ???? Any inputs on the same !!



  • dimagordimagor Member Posts: 16
    [b][red]This message was edited by dimagor at 2003-4-25 6:19:23[/red][/b][hr]
    try this:

    [code]
    uses ... ShellAPI;

    ....

    function TfrmMainDlg.DelDir(dir: string): Boolean;
    var
    fos: TSHFileOpStruct;
    begin
    ZeroMemory(@fos, SizeOf(fos));
    with fos do
    begin
    wFunc := FO_DELETE;
    fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
    pFrom := PChar(dir + #0);
    end;
    Result := (0 = ShFileOperation(fos));
    end;
    [/code]

    it works fine for me.


  • TiefTief Member Posts: 179
    : [b][red]This message was edited by dimagor at 2003-4-25 6:19:23[/red][/b][hr]
    : try this:
    :
    : [code]
    : uses ... ShellAPI;
    :
    : ....
    :
    : function TfrmMainDlg.DelDir(dir: string): Boolean;
    : var
    : fos: TSHFileOpStruct;
    : begin
    : ZeroMemory(@fos, SizeOf(fos));
    : with fos do
    : begin
    : wFunc := FO_DELETE;
    : fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
    : pFrom := PChar(dir + #0);
    : end;
    : Result := (0 = ShFileOperation(fos));
    : end;
    : [/code]
    :
    : it works fine for me.
    :
    :
    :

    you can also try this

    [code]
    //*****************************************************************************
    function DeleteDir(const Directory : String) : Boolean;
    var
    FileFound : TSearchRec;
    begin

    if Not DirectoryExists(Directory) then
    begin
    Result := False;
    Exit;
    end;

    if FindFirst(Directory,faAnyFile,FileFound) = 0 then
    begin
    if Not SysUtils.DeleteFile(Directory + '' + FileFound.Name) then
    begin
    Result := False;
    Exit;
    end;

    while FindNext(FileFound) = 0 do
    begin
    if Not SysUtils.DeleteFile(Directory + '' + FileFound.Name) then
    begin
    Result := False;
    Exit;
    end;
    end;
    end;

    SysUtils.FindClose(FileFound);

    if Not RemoveDir(Directory) then
    Result := False
    else
    Result := True;
    end;
    [/code]

Sign In or Register to comment.