Thread: How can i check a directory for certain files?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    How can i check a directory for certain files?

    Hello to all.
    I want to check a directory, lets call it "ProgramDirectory", for certain files, which we will say are all called filename#.file where # is a number, to see if they already exist. The program is in the directory it is checking but i would also like to know how to do this if it was in a different dircetory. I dont want to do this by opening a file stream and then running a
    Code:
    if(!ifstream_variable.is_open)
    {etc.};
    because I want to use the directory search to tell the program what files already exist for it to open. The search needs to be able to search progressively through the file numbers. for example it needs to be able to check if filename1.file exists and if it does then it will check to see if filename2.file exists and so on until a file number doesnt exist. I would also like to know how to search a directory for any filename I specify. I know this is a lot of questions but I greatly appreciate any help. any additional information is welcome and appreciated as well. Also any information even partially pertaining to the question is helpful.

    Thanks in advance.

    PS
    I am new to this forum so if i did something wrong (which i probably did) then i apologize and please let me know so i can fix it or at the least not do it next time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There's a FAQ entry on how to read all the filenames in a given directory. You could use that to scan for filenames which match the pattern you're looking for, and record the maximum value of 'n' you see.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You've not given a proper reason why trying to open the file isn't a valid way of writing the program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Thanks Salem, I did look through the FAQ before posting this but I guess I just missed it. I'll have to find that today.

    Writing the program like that would work perfectly fine. The reason I ask is because I don't know how to do it this way as so I want to learn. In fact I have already written the program once using that but after my HD broke I lost the program so now im rewriting with comparatively much more advanced code. Comparatively being the key word here. (The first time I wrote it I was almost completely new to C++.)

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    FindFirstFile() and FindNextFile() under Win32. I'm not sure how to do it under linux.

    You can use wildcards like *.* for the filename and it will return every file in the directory.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    So I looked over the FAQ again and found the right one. The only problem is I am not a level 3. I am about a 1.5 but I'm getting there. And so abachler I don't yet know Win32.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you'll either have to learn to swim, or drown... Because doing this sort of thing requires some ability. Level 3 is still fairly "low" in C++, I'd say...

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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just wondering, what is a level 3 or level 1.5?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think it refers to the FAQ's "Level 1" etc.

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

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Yeah its talking about the FAQ levels and I am really new. But hey you gotta start somewhere. I was wondering how long it took ya'll to become masters of the C++?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Having worked professionally in C++ for 6 months, I wouldn't call myself a master. But I have 20 years experience of programming, and still learning there too! In those 20 years, about 15 have been in C.

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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by patrioticpar883 View Post
    Yeah its talking about the FAQ levels and I am really new. But hey you gotta start somewhere. I was wondering how long it took ya'll to become masters of the C++?
    I started messing with C++ about 8 years ago or so. But before that I used Visual Basic. Haha. Now that you think back on it, how much VB sucks
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The first C program I wrote was in '93. It didn't do much. Windows 3.1 I think with Program Manager. Fun stuff. Then, I did nothing with it again until picking it back up last summer. I still have Borland C++ 3.1 in the box with all the manuals.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I learned C++ about 7 or 8 years ago. I consider myself a master of theoretical knowledge, though I lack practical experience in large projects.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 02:02 PM
  3. Copying all files in a directory
    By rak1986 in forum C Programming
    Replies: 2
    Last Post: 08-25-2008, 01:02 AM
  4. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  5. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM