Thread: Open first file in folder

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    18

    Open first file in folder

    Hello, I am a total newbie at C programming and I am trying to make a program that renames every file in a directory. I want it to rename every file by deleting the first x amount of characters from each file name. So lets say, I input 3, and it will delete the first 3 characters from every file name in the directory. Is there a way to do this? I am currently stuck at being able to open a file without inputting a file name. I want it to just do it to every file in the directory, one by one until there are no more files to be renamed in that particular directory. Like I said, I am trying to use rename() but that requires the name of the file that is being renamed to be inputted (if that's even a word).

    Thank you =]

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Check the FAQ (listed near the top of the page). It has an entry on directory reading.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    The Boost Filesystem Library (Filesystem Home) is free (you may already have it installed; otherwise you can download it) and includes functions that will let you easily access the filenames in your program and has the advantage that your code will be portable to various platforms.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    However, I note that Boost.Filesystem is a C++ library.
    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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by laserlight View Post
    However, I note that Boost.Filesystem is a C++ library.
    Wonderful. Please elaborate. That must be part of the "TR2" stuff which (for those of use who are not professional programmers) is a well-kept secret. For example, I tried
    Code:
    using std::tr2::sys;
    in a sample program I found in the boost_filesystem online documentation, but got nowhere with that. I haven't found anything about it at cppreference.com or cplusplus.com. Would you please post any links to references for the Boost components that have been added to C++, including how to #include them and the correct namespace directives.

    PS: It doesn't seem to be part of GCC 4.2. I had to explicitly install the boost-dev and boost-filesystem-dev packages to access these libraries.

    PPS: I finally worked out that I could either use
    Code:
    using namespace boost::filesystem;
    or use an alias as in
    Code:
    fs = namespace boost::filesystem
    and then prefix each reference to a filesystem function with "fs::".

    PPPS: Sorry to be so dense about the C vs. C++ point.
    Last edited by R.Stiltskin; 01-18-2010 at 04:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM