Joseph Smith
Joseph Smith

Reputation: 139

How to iterate over directory and perform task on each individual file

I've the ability to list files within a given directory, however I'm wanting to isolate each file found within the directory before it then reads the next one, to then do something to it (e.g. change its name).

I'm just curious how I could monitor a directory (so that the function does not end, instead listing files that are added to the directory), and also the above to do something with each file in the order they are found.

int main(void)
{    
    struct dirent* de;  // Pointer for directory entry 

// opendir() returns a pointer of DIR type.  
DIR* dr = opendir("E:\\Users\\Joe\\Downloads");

if (dr == NULL)  // opendir returns NULL if couldn't open directory 
{
    printf("Could not open current directory");
    return 0;
}

// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html 
// for readdir() 
while ((de = readdir(dr)) != NULL)
    printf("%s\n", de->d_name);

closedir(dr);
return 0;
}

If there are any Windows specific methods to doing this I would appreciate an example as I'm very much struggling to create my own. Thank you in advance.

Upvotes: 0

Views: 103

Answers (1)

ryyker
ryyker

Reputation: 23236

FindFirstFile() and FindNextFile() are the Windows specific methods for doing what you want...

In the following adaptation of this example, I have commented where the file names found can be worked. (i.e. name changed, opened/edited, etc.) Look for "/// do your work here...".

Enter a directory path on the command line such as C:\\dir1\\dir2, and this will find all files in that location.

Note, this adaptation removes some of the inane Microsoft methods for copying, and concatenating strings.

#define MAX_PATHNAME_LEN 260

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   DWORD dwError;
   char DirSpec[MAX_PATHNAME_LEN];
   size_t length_of_arg;


   // Check for command-line parameter; otherwise, print usage.
   if(argc != 2)
   {
      printf("Usage: Test <dir>\n");
      return 2;
   }

   // Check that the input is not larger than allowed.
   length_of_arg = strlen(argv[1]);
   if (length_of_arg > (MAX_PATHNAME_LEN - 2))
   {
      printf("Input directory is too large.\n");
      return 3;
   }

   printf ("Target directory is %s.\n", argv[1]);

   // Prepare string for use with FindFile functions.  First, 
   // copy the string to a buffer, then append '\*' to the 
   // directory name.
   sprintf(DirSpec, "%s\\*", argv[1]); 

   // Find the first file in the directory.
   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);  "/// do your work here..."

                        // List all the other files in the directory.
      while (FindNextFile(hFind, &FindFileData) != 0) 
      {
         printf ("Next file name is %s.\n", FindFileData.cFileName); "/// do your work here..." 
      }

      dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u.\n", dwError);
         return (-1);
      }
   }

   return (0);
}

Upvotes: 2

Related Questions