Thread: Read all files within a folder?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    5

    Lightbulb Read all files within a folder?

    Hi everyone, first time poster here. I'm writing some code and have been using this forum to help, but I haven't been able to find a solution to my specific issue. I'm writing a code that basically ends up saving data on a text file, within Folder x, within Folder 1. Folder 1 is a set folder, however the Folder x and the text files both have variables in their names and have no sort of order or anything to them. So far I have been using sprintf to combine the set file path (that takes me to Folder 1) and then adding the strings to the back end of it to specify Folder x's name and the name of the .txt.

    What I want to do is make an .exe that can run, scan all the text documents from Folder x within Folder 1, and once they find a specific value they will prompt the user to go check the file. That part isn't so important, I just need some guidance on how to scan all the different .txt's within a folder within a folder.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    5
    Thanks for the link.

    This code from that link is the beginning of what I'm looking for

    Code:
    #include <dirent.h> #include <stdio.h> 
    
    int main(void)
    {
      DIR           *d;
      struct dirent *dir;
      d = opendir(".");
      if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
          printf("%s\n", dir->d_name);
        }
    
        closedir(d);
      }
    
      return(0); }
    however this code simply reads the Folder names from Folder x, how would I make this program investigate the .txt files within the folder?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You keep reading the FAQ.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    5
    Mhmm. Thanks, helpful.

    The FAQ just has different versions of the code that don't work in my compiler. I need to know what to add to the code I posted, since it works, to just dive down another level.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post some actual details - error messages, name of compiler, whatever.

    "It doesn't work" doesn't tell us anything we can use to help you.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    5
    Im not getting any error messages, I didn't say it didn't work. I'm using DevC++ on a windows 7 pc. The code I posted from the FAQ performs fine, it looks into the directory (folder 1) and prints me out the names of all of the files. I just need to know how to add on to that code a little piece that says "if this is folder x (within folder 1), go in and print all of the names of files within that folder." And then exit the subfolder x back into filder 1 and continue as it was before.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're using bloodshed dev-c++, I suggest you get rid of it.

    Then get this up to date clone of it.
    Dev-C++ download | SourceForge.net

    Since you're on Windows, you need the Win32 API, not the POSIX API.
    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.

  9. #9
    Registered User
    Join Date
    Nov 2015
    Posts
    5
    Yeah I have the most recent version of Dev. I tried the Win32 and it works. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2012, 03:25 PM
  2. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  3. Read in a Folder to a String Array
    By Coding in forum Windows Programming
    Replies: 12
    Last Post: 02-09-2008, 03:09 PM
  4. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 09:18 AM
  5. get files in a folder and more
    By appleGuy in forum Windows Programming
    Replies: 3
    Last Post: 09-05-2006, 08:59 AM

Tags for this Thread