Link to home
Start Free TrialLog in
Avatar of JohannRamon
JohannRamonFlag for Colombia

asked on

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

Avatar of JohannRamon
JohannRamon
Flag of Colombia image

ASKER

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
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.

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.

ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pgnatyuk,

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

Johann
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