CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Posts
    9

    How to delete all files in a directory?

    Hi,

    How do I delete all files in a directory? Right now I am using
    system("del c:\directory\*.*)
    but it shows a DOS box which is not desirable




  2. #2
    Join Date
    Apr 1999
    Posts
    383

    Re: How to delete all files in a directory?

    Use FindFirstFile/FindNextFile with DeleteFile.

    Dave


  3. #3
    Join Date
    May 1999
    Location
    Houston - TX - US
    Posts
    29

    Re: How to delete all files in a directory?

    Hi,

    below is the code to delete the directory!

    BOOL DeleteDirectory( CString DirectoryPath )
    {

    if ( DirectoryPath.IsEmpty()) //make sure not to delete the Root !
    return TRUE ;

    CFileFind finder;
    DirectoryPath += "\\*.*";

    BOOL bWorking = finder.FindFile(DirectoryPath);

    while (bWorking)
    {
    bWorking = finder.FindNextFile();
    if (!finder.IsDots()) //skip the dots which denote the current directory
    {
    DeleteFile((LPCTSTR)finder.GetFilePath());
    if (finder.IsDirectory())
    {
    DeleteDirectory(finder.GetFilePath());
    }
    }
    }
    finder.Close();

    if (!RemoveDirectory(LPCTSTR( DirectoryPath.Left(DirectoryPath.GetLength()-strlen("\\*.*")) + '\0')))
    {
    return FALSE;
    }
    return TRUE;
    }


    Hope this helps You.

    Good Luck.
    Yash.




  4. #4
    Join Date
    Apr 1999
    Posts
    9

    Thank You!

    Hi,

    Thank you all for the prompt replies.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured