Thread: how to modify this func so that it would also delete folders?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    how to modify this func so that it would also delete folders?

    How to modify this function so that It would also delete all directories (including empty ones) in given directory?

    Code:
    void DLL_EXPORT DeleteAllFiles(const LPCSTR folderPath)
    {
    MessageBoxA(0, folderPath, "DLL Message", MB_OK | MB_ICONINFORMATION);
     char fileFound[256];
     WIN32_FIND_DATA info;
     HANDLE hp;
     sprintf(fileFound, "%s\\*.*", folderPath);
     hp = FindFirstFile(fileFound, &info);
     do
        {
           sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
           DeleteFile(fileFound);
    
        }while(FindNextFile(hp, &info));
     FindClose(hp);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Check attributes of each file to see if it is a directory. Whenever a directory is found, delete the files in that directory before deleting the directory itself. One way of doing that is for your function to call itself recursively.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    could u modify my func? I am beginner and I cand to it thatway cuz I just have no idea what to next.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Something like...

    Code:
    void DLL_EXPORT DeleteAllFiles(const LPCSTR folderPath)
    {
    MessageBoxA(0, folderPath, "DLL Message", MB_OK | MB_ICONINFORMATION);
     char fileFound[256];
     WIN32_FIND_DATA info;
     HANDLE hp;
     sprintf(fileFound, "%s\\*.*", folderPath);
     hp = FindFirstFile(fileFound, &info);
    what if there are no files found???
    do
    {
           sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
    
          //is this a folder?
           //use the dwFileAttributes to find the folders
           //if it is a folder, call DeleteAllFiles(fileFound)
           //else
    
            DeleteFile(fileFound);
    
    }while(FindNextFile(hp, &info));
     FindClose(hp);
    }
    You should be checking for GetLastError() == ERROR_NO_MORE_FILES
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Quote Originally Posted by novacain View Post
    Something like...

    Code:
    void DLL_EXPORT DeleteAllFiles(const LPCSTR folderPath)
    {
    MessageBoxA(0, folderPath, "DLL Message", MB_OK | MB_ICONINFORMATION);
     char fileFound[256];
     WIN32_FIND_DATA info;
     HANDLE hp;
     sprintf(fileFound, "%s\\*.*", folderPath);
     hp = FindFirstFile(fileFound, &info);
    what if there are no files found???
    do
    {
           sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
    
          //is this a folder?
           //use the dwFileAttributes to find the folders
           //if it is a folder, call DeleteAllFiles(fileFound)
           //else
    
            DeleteFile(fileFound);
    
    }while(FindNextFile(hp, &info));
     FindClose(hp);
    }
    You should be checking for GetLastError() == ERROR_NO_MORE_FILES
    It does same thing ass my code, it deletes all files but no folders. I need modification to get it deletefiles and folders, both.... it must clear given directory.

    and how do I use dwFileAttributes?

    sorry if i ask stupid questions but I really dont know it cuz I haven't done anything like this before.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use a debugger to find where it fails and post your attempt at it.

    There are only a few lines missing.

    I will not help again utill I see you have at least made an effort to solve the problem.

    EDIT: dwFileAttributes is an element of the WIN32_FIND_DATA structure. It will tell you if the current 'found' object is a file or a folder. Look it up in MSDN.
    Last edited by novacain; 12-25-2009 at 09:49 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    debugger? u meant thatthing tat show error when u compile stuff? There's no errors.

    Code:
    -------------- Build: Debug in testdll ---------------
    
    Compiling: main.cpp
    Linking dynamic library: bin\Debug\testdll.dll
    Creating library file: bin\Debug\libtestdll.a
    Output size is 1.26 MB
    Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 0 warnings
    Your I dea was pretty good but there was 1 small problem, Instead of going upwards in dir tree it went backwards

    I executed ths dll func in D:\My Documents\cpp\dll\testdll\bin\Debug\New folder
    and it deleted all files in.
    1. New folder
    2. Debug
    3. bin
    4. testdll
    5. dll
    6. cpp
    7. My Documents


    I lost all files from my documents :S I even don't know if I had there something valueable but it didn't delete the dirs.

    But bigger problem is that
    D:\My Documents\cpp\dll\testdll\bin\Debug\New folder\test\New folder (2)\ still contains it's files. how to fix that?

    That's the code.
    Code:
    void DLL_EXPORT DeleteAllFiles(const LPCSTR folderPath)
    {
    //MessageBoxA(0, folderPath, "DLL Message", MB_OK | MB_ICONINFORMATION);
     char fileFound[256];
     WIN32_FIND_DATA info;
     HANDLE hp;
     sprintf(fileFound, "%s\\*.*", folderPath);
     hp = FindFirstFile(fileFound, &info);
    //what if there are no files found???
    do
    {
           sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
    
          //is this a folder?
           //use the dwFileAttributes to find the folders
           //if it is a folder, call DeleteAllFiles(fileFound)
           //else
           WIN32_FIND_DATA FindFileData;
           if ( FindFileData.dwFileAttributes )
           {
              //MessageBoxA(0, fileFound, "Folder dedected", MB_OK | MB_ICONINFORMATION);
              DeleteAllFiles(fileFound);
           }
    
            DeleteFile(fileFound);
    
    }while(FindNextFile(hp, &info));
     FindClose(hp);
    }
    I don't know it I think in right direction but I think I need method to skip action if path ends with . or .. but I have no idea how to do that.

    edit after few secs DLL crashed (i think it happened when it tried to clear D:\
    D:\ contains my data, there's no system prodected files, dno why it didn't totch it ..
    Last edited by rain-13; 12-26-2009 at 09:17 AM. Reason: added some info

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You copied this code from the 'net?
    No idea how it works and not taken the time to RTFM either.
    You will not get very far without spending considerable time reading MSDN etc.


    A 'Compiler' checks code and finds errors and warnings.
    A 'debugger' allows you to step thru the code one line at a time as the program runs, watching what happens.
    (in MSVC, highlight a line and press F9. A 'break-point' (red dot) appears. Run the app and it will stop at the break point, F10 will step one line forward etc)


    When you call FindFile() or FindNextFile() it fills in data about the 'found file' into the WIN32_FIND_DATA struct you sent in as a param (WIN32_FIND_DATA info; in this case)

    All folders contain two special folders, '.' and '..'

    . means go up one folder
    .. means go down one folder

    So if we are in 'D:\My Documents\cpp\dll\testdll\bin\Debug\New folder' and you ask to delete all the file in '.\*.*' it will delete all files from 'D:\My Documents\cpp\dll\testdll\bin\Debug'

    You need to ignore these folders.
    IF this 'find' is a folder AND the first character in the file name is a '.', then ignore this 'find' and call FindNextFile()

    Look at 'RemoveDirectory()' when you want to delete an empty folder (folders with file in will not delete)
    Make sure you check to see if it was successful in deleting the folder (check the return and call getLastError() ).

    Code:
    //is this a folder?
    if(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
         //this is a folder
         //is it the '.' or '..' folder? 
         [test the first character is not a '.']
    EDIT: Please do not use LEET ('u', 'cuz' etc). Many of us old timers just ignore posts in LEET as we consider it inconsiderate to people who do not use/learn English as their first language.
    Last edited by novacain; 12-26-2009 at 10:10 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by novacain View Post
    You copied this code from the 'net?
    Looks like so, though in the most blatant act of overlook I've ever seen, (s)he didn't look at the very next paragraph which deals with this very problem. Anyhoo, as you'd expect, it's been spammed all over and he's in the process of causing much consternation from the DIY brigade at the MS forums.

    Quote Originally Posted by novacain View Post
    All folders contain two special folders, '.' and '..'

    . means go up one folder
    .. means go down one folder
    . is the current folder
    .. is go up one folder

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  2. how to delete or modify a record in a file?
    By danielhf in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2003, 09:18 AM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM