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

Hi:

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,

Johann

#define _WIN32_WINNT 0x0501

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

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   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);
   } 
   else 
   {
      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();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   return (0);
}

Open in new window

LVL 2
JohannRamonAsked:
Who is Participating?

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

x
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];
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;

Johann
0
pgnatyukCommented:
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:
http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx

There is a link to another example.
http://msdn.microsoft.com/en-us/library/aa365200(v=VS.85).aspx

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

0
pgnatyukCommented:
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.

0
Angular Fundamentals

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

pgnatyukCommented:
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:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_10185091.html

Here you will find another example:
http://www.ucancode.net/Visual_C_Codes/Get-file-folder-time-set-file-folder-time-FindFirstFile-FindNextfileGetFileAttributes-vc-example.htm

0

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:
pgnatyuk,

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

Johann
0
JohannRamonAuthor Commented:
pgnatyuk:

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,

Johann
0
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
C

From novice to tech pro — start learning today.