List files in a folder but only files that starts with a number

This is a discussion on List files in a folder but only files that starts with a number within the C Programming forums, part of the General Programming Boards category; I am trying to make a program that can scan a folder, and then list all files that starts with ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    List files in a folder but only files that starts with a number

    I am trying to make a program that can scan a folder, and then list all files that starts with a number. Please help
    I am trying to base my idea in this example code.

    Code:
    int main(void)
    {
      DIR           *d;
      struct dirent *dir;
      d = opendir("/directory");
      if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
         printf("%s\n", dir->d_name);
        }
    
        closedir(d);
      }
    
      return(0);
    }
    The above program prints all files in a directory. Can someone help me how to filter the list files so then it will only print files starting with numbers?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    The d_name element contains the name as a string, which can be treated much like a char array. Thus, you can check the first character, dir->d_name[0] to see if it is a digit. Use the isdigit function, and remember to #include <ctype.h>.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    that make sense. tnx i will try this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  2. Count number of files in a folder
    By parimal.s.dave in forum C Programming
    Replies: 3
    Last Post: 07-11-2011, 05:24 AM
  3. Number of files in a folder
    By gadu in forum C Programming
    Replies: 1
    Last Post: 10-09-2008, 06:16 PM
  4. Getting List of files in particular folder
    By Bargi in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2007, 04:00 AM
  5. ...how to get the number of files in a folder
    By toby1909 in forum Windows Programming
    Replies: 5
    Last Post: 02-01-2002, 04:43 PM

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