Finding files in a directory

This is a discussion on Finding files in a directory within the C Programming forums, part of the General Programming Boards category; I'm trying to write code that searches through a folder and retrieves the names of the files in the folder. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    Finding files in a directory

    I'm trying to write code that searches through a folder and retrieves the names of the files in the folder.

    So far I have the following code. When I run it, I just keep getting the Usage portion of the code. Why does argc need to be equal to 2 for the code to not print the usage portion. Also, where in the code would I specify the name of the directory I want to search?

    Code:
    #define _WIN32_WINNT 0x0501
    
    #include <windows.h>
    #include <tchar.h> 
    #include <stdio.h>
    #include <strsafe.h>
    #include <conio.h>
    
    int _tmain(int argc, TCHAR *argv[])
    {
       WIN32_FIND_DATA ffd;
       TCHAR szDir[MAX_PATH];
       size_t length_of_arg;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError=0;
      
       // If the directory is not specified as a command-line argument,
       // print usage.
    	
       if(argc != 2)
       {
           _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
          return (-1);
       }
    
       // Check that the input path plus 2 is not longer than MAX_PATH.
    
       StringCchLength(argv[1], MAX_PATH, &length_of_arg);
    
       if (length_of_arg > (MAX_PATH - 2))
       {
          _tprintf(TEXT("Directory path is too long.\n"));
          return (-1);
       }
    
    	_tprintf (TEXT("Target directory is %s\n\n"), argv[1]);
    
       // Prepare string for use with FindFile functions.  First, copy the
       // string to a buffer, then append '\*' to the directory name.
    
       StringCchCopy (szDir, MAX_PATH, argv[1]);
       StringCchCat (szDir, MAX_PATH, TEXT("\\*"));
    
       // Find the first file in the directory.
    
       hFind = FindFirstFile(szDir, &ffd);
    
       if (INVALID_HANDLE_VALUE == hFind) 
       {
          dwError = GetLastError();
    
          _tprintf (TEXT("FindFirstFile failed (%u)\n"), dwError);
          return dwError;
       } 
       else 
       {
          _tprintf (TEXT("First file: %s\n"), ffd.cFileName);
       
          // List all the other files in the directory.
    
          while (FindNextFile(hFind, &ffd) != 0) 
          {
             _tprintf (TEXT(" Next file: %s\n"), ffd.cFileName);
          }
        
          dwError = GetLastError();
          if (dwError != ERROR_NO_MORE_FILES) 
          {
             _tprintf (TEXT("FindNextFile error (%u)\n"), dwError);
             goto Cleanup;
          }
       }
    
    Cleanup:
       FindClose(hFind);
       return dwError;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first argument of the command-line is the directory specification. You can specify that with "myprog somedir" if you use a command prompt to run your code, or if you use visual studio or similar, you need to set the command line argument somewhere in the project config settings - I find it much easier to use the command prompt, in 99% of the case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 01:02 PM
  2. Finding files question
    By geek@02 in forum Windows Programming
    Replies: 12
    Last Post: 09-02-2008, 03:31 AM
  3. Copying all files in a directory
    By rak1986 in forum C Programming
    Replies: 2
    Last Post: 08-25-2008, 01:02 AM
  4. deleting all files in a directory using c..
    By ShadeS_07 in forum C Programming
    Replies: 6
    Last Post: 07-30-2008, 08:21 AM
  5. R/W on files in a directory by i-number
    By Lateralus in forum Linux Programming
    Replies: 1
    Last Post: 07-26-2005, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21