Thread: Reading files in a directory

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Reading files in a directory

    The simple question: Is there an elegant way of readding all the files in a directory? If anybody if familiar with Perl, I'm looking for something like readdir().

    The reason why I want to know: I'm writing a program that compiles data from several different files (all of the same format). The idea is that data entry can take place on a number of different machines at the same time and saved to a file on each machine. Then all the files would be put together and this program (that I'm currently writing) would go through each one in the same manner and read the data.

    I already have a mechanism for reading the data from a single file, and I believe it's easily expandable with more than one file. My problem is how to find and open those files, which may be called

    data0001.dat
    data0002.dat
    :
    data1234.dat, etc.

    This is why I'm looking for a way to read the files in a directory, instead of trying all 10,000 variations on this particular name (especially if I decide not to use numbers in the file name). Thanks in advance for any help!

    --dave

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    why wouldn't you post this in one of the programming boards? This is more of a hardware problem board.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you'd have said which OS you were using, I'd have moved the thread for you, and would probably have answered it, but as you don't, <fx> shrugs shoulders </fx>, what can I say?

    Whatever OS it is, the answer is almost certainly "yes" you can do it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    I did put it on the C++ Programming board, but it was moved here (I don't know why). I'm using Windows 98 and XP, but if possible I'd like to find a portable solution.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Now it's on the Windows board - very mobile thread this! This is the right place for programming questions that are Windows specific.

    To list the files in a directory, you would use the FindFirstFile()/FineNextFile() API routines. These will work for pretty much any Windows system this side of the Ark. Look them up in your compilers help or at MSDN...

    http://msdn.microsoft.com/library/default.asp

    ... you won't find a standard routine for doing this across all OS's as it is "OS specific".

    I've written you an example using FindFirst/Next()...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> I've written you an example using FindFirst/Next()...

    ... which didn't seem to want to attach, let's try again...

    *** EDIT ***

    >>> Yes, even some versions of windows are compatible with POSIX

    True, bit not all OS's are POSIX compliant
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a directory --code from FAQ
    By the bassinvader in forum C Programming
    Replies: 5
    Last Post: 11-11-2006, 02:40 PM
  2. Opening all files in a directory
    By TKMan in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 01:27 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. getting files in directory
    By threahdead in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:13 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM