• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

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

0
JohannRamon
Asked:
JohannRamon
  • 3
  • 3
1 Solution
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now