Thread: Looping through directories and printing the files and contents of subdirectories.

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Looping through directories and printing the files and contents of subdirectories.

    Note: Can't use dirent.h library.
    I need to modify my program to print files and subdirectories in a directory. Any help would be greatly appreciated.
    For example this is what the output should look like:
    <DIR> U:\Test\
    sample
    (2).txt 6
    sample
    .txt 6

    <DIR> U:\Test\New folder (5)\
    sample
    (4).txt 6
    sample
    (3).txt 6
    sample
    (2).txt 6
    sample
    .txt 6

    <DIR> U:\Test\New folder (4)\
    sample
    (5).txt 6
    sample
    (4).txt 6
    sample
    (3).txt 6
    sample
    (2).txt 6
    sample
    .txt 6

    <DIR> U:\Test\New folder (3)\
    sample
    .txt 6

    <DIR> U:\Test\New folder (2)\
    sample
    (4).txt 6
    sample
    (3).txt 6
    sample
    (2).txt 6
    sample
    .txt 6

    <DIR> U:\Test\New folder\
    sample
    -Copy(2).txt 6
    sample
    -Copy.txt 6
    sample
    .txt 6This is what I have so far which only lists the files and subdirectories. I need to able to open the subdirectories and print the associated files. I'm trying to search and print files in seperate functions.
    Code:
    WIN32_FIND_DATA ffd;
        HANDLE          hFind = INVALID_HANDLE_VALUE;
        size_t          lengthOfArg;
        TCHAR           szDir[MAX_PATH];
        LARGE_INTEGER   fileSize;
        DWORD           dwError;
        FILE*fp;
    
    
    do
        {
            // did we find a directory?
            // ffd.dwFileAttributes says this is a directory (FILE_ATTRIBUTE_DIRECTORY)
            if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    
    
                _tprintf(_T("%s  <DIR>\n"), ffd.cFileName);
    
            // did we find a file?
            else
            {
                fileSize.LowPart= ffd.nFileSizeLow;
                fileSize.HighPart= ffd.nFileSizeHigh;
                _tprintf(_T("%s   %ld\n"), ffd.cFileName, fileSize.QuadPart);
            }
    
            // continue the search; try to find more files
        }while(FindNextFile(hFind,&ffd)!=0);
    



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,741
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out contents of bit array
    By sigur47 in forum C Programming
    Replies: 4
    Last Post: 11-22-2011, 04:39 PM
  2. Replies: 1
    Last Post: 10-23-2011, 11:50 AM
  3. Printing contents of a text file
    By never_lose in forum C Programming
    Replies: 10
    Last Post: 04-28-2011, 09:25 AM
  4. printing array contents
    By c_programmer in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 06:00 PM
  5. Printing the contents of memory
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-07-2006, 01:51 PM

Tags for this Thread