Thread: Iterate through every file in a directory

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Iterate through every file in a directory

    Can someone give me a clue as to how to do this on a Windows system? I just need to be able to process each file of a directory in turn, the directory having been passed as a command line argument.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's described in the FAQ.

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

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Where? All I can find is a short FAQ relating to file i/o in C++.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The FAQ only shows how to do this in linux.

    From MSDN:
    Code:
    #include <windows.h>
    #include <tchar.h> 
    #include <stdio.h>
    #include <strsafe.h>
    
    void ErrorHandler(LPTSTR lpszFunction);
    
    int _tmain(int argc, TCHAR *argv[])
    {
       WIN32_FIND_DATA ffd;
       LARGE_INTEGER filesize;
       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("\nUsage: %s <directory name>\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("\nDirectory path is too long.\n"));
          return (-1);
       }
    
       _tprintf(TEXT("\nTarget 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) 
       {
          ErrorHandler(TEXT("FindFirstFile"));
          return dwError;
       } 
       
       // List all the files in the directory with some info about them.
    
       do
       {
          if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          {
             _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
          }
          else
          {
             filesize.LowPart = ffd.nFileSizeLow;
             filesize.HighPart = ffd.nFileSizeHigh;
             _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
          }
       }
       while (FindNextFile(hFind, &ffd) != 0);
     
       dwError = GetLastError();
       if (dwError != ERROR_NO_MORE_FILES) 
       {
          ErrorHandler(TEXT("FindFirstFile"));
       }
    
       FindClose(hFind);
       return dwError;
    }
    
    
    void ErrorHandler(LPTSTR lpszFunction) 
    { 
        // Retrieve the system error message for the last-error code
    
        LPVOID lpMsgBuf;
        LPVOID lpDisplayBuf;
        DWORD dw = GetLastError(); 
    
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dw,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );
    
        // Display the error message and exit the process
    
        lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
            (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
        StringCchPrintf((LPTSTR)lpDisplayBuf, 
            LocalSize(lpDisplayBuf) / sizeof(TCHAR),
            TEXT("%s failed with error %d: %s"), 
            lpszFunction, dw, lpMsgBuf); 
        MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
    
        LocalFree(lpMsgBuf);
        LocalFree(lpDisplayBuf);
    }

  5. #5
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Thanks! So it appears to be a little more involved than the Python method then:
    Code:
    for <file> in <directory>:

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Just about everything in C will take more code than it does in Python. Nature of the beast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM