Thread: How to get names to all files/folders in a folder?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    9

    How to get names to all files/folders in a folder?

    Hi all!
    I am quite new to ANSI C so here comes a newbee question:
    How to get names to all files/folders in a folder without knowing their names?
    fopen() workes fine when you know the name of the file.
    As far as I have found out this isn't supported in the library but what is the way to solve it?

    Many thanks
    Marcux

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't know of a standard way of doing that. These seems more like a script kind of thing. Why do you want to do this ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    I think the library you need is dirent.h
    this will give you functions like opendir() and readdir()
    You might also need sys/stat.h for stat()

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    I have got an application that in a specific situation produces files in a folder and will not stop untill hd is full ( a bugg ofcause) but til the owner of that software has fixed the bugg I want to write a small app that checkes the folder and if it is not empty delete all files. That is why I want to find a way to "browse" a folder and I can imagine that there are more situations that you are interested in finding out the contents of a folder.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your platform and compiler?
    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

  6. #6
    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 always the FAQ...

    But if all you're interested in is deleting all the files in a given folder, then a 1-line command file will do the trick.

    You can even schedule it to do it automatically.
    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. 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. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  3. Hiding a folder
    By nextstep in forum Windows Programming
    Replies: 16
    Last Post: 03-20-2005, 03:07 PM
  4. how to get names of files in folder
    By kiku in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2005, 06:51 PM
  5. is there a way to write the file names in a folder in a text file?
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-15-2002, 05:11 PM