Using Visual C++ how can i list and remove one by one all folders on an especific path?


I need to list and depending on certain condition remove the folders (and its contents) that exists on certain path. and cannot use .Net namespaces, just plain C++ functions.
I searched on MSDN and found "FindFirstFile", "FindNextFile" and "remove" functions, however in the sample code from MSDN i don't understand some simple things like the way to change the code for listing only folders instead of files and the way to assign my path to the parameter. ¿How can i do that?

For setting the path on the code should i use something like this?
char *argv = "C:\My\Path";

And how can i check the FILE_ATTRIBUTE_DIRECTORY of the results (before or after calling FindFirstFile)?

I know this can look simple if you have experience with files and folders from VC++ but in my work i deal most of the time with DB so this is obscure for me.

Thanks and regards,


#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
   WIN32_FIND_DATA FindFileData;
   char DirSpec[MAX_PATH];  // directory specification
   DWORD dwError;

   printf ("Target directory is %s.\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE) 
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
      printf ("First file name is %s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0) 
         printf ("Next file name is %s\n", FindFileData.cFileName);
      dwError = GetLastError();
      if (dwError != ERROR_NO_MORE_FILES) 
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
   return (0);

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohannRamonAuthor Commented:
Forgot to mention that MSDN says the structure WIN32_FIND_DATA has this contents:

typedef struct _WIN32_FIND_DATA {  DWORD dwFileAttributes;  FILETIME ftCreationTime;  FILETIME ftLastAccessTime;  FILETIME ftLastWriteTime;  DWORD nFileSizeHigh;  DWORD nFileSizeLow;  DWORD dwReserved0;  DWORD dwReserved1;  TCHAR cFileName[MAX_PATH];  TCHAR cAlternateFileName[14];

Can you give more details? I need to understand what exactly you need. What is the goal?
This function FindFileFirst works fine. There is one more function FindNextFile. Usually you need to have a loop.
Probably, you saw this page from MSDN:

There is a link to another example.

You will see there (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
This is the way to know that the current item is a directory.

If I understand the task, I'd propose to read the input folder first and save in an array all folders and files you need to delete. Then, in another loop, I'd delete all files and then all folders (beginning from the deepest one). It allows to avoid a recursive function that deletes a folder with the sub-folders.

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

In you case, you can to add to the loop this condition
(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

Probably, this thread has the function you need:

Here you will find another example:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohannRamonAuthor Commented:

Thanks for your prompt answers and alternatives, i've just read them and begin changing the code. I will post again soon.

JohannRamonAuthor Commented:

I´m sorry for the long delay in posting again, as usual in my work priorities change and let me without the required time for testing solutions.

Thanks for your help with this issue and best regards,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.