Thread: Tons of File/Directory Manipulation.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    32

    Tons of File/Directory Manipulation.

    I have read into the FindFirstFile and FindNextFile functions for windows extensively, and read numerous threads on the topic, but can't seem to break it down and figure out how to use it for what I need it to do. I am looking for any sort of help or further guidance so I can get started on this part of my project.

    The following is what I need to accomplish:
    1. I need to access and create a list of all of the sub-directories within the programs current directory.
    2. I need to access and create a list of all of the files within each sub-directory.
    3. I need to rename the files within each sub-directory numerically, i.e. 1, 2, 3, 4... etc (optional, but will make life easier).
    4. I need to be able to move the files from within the sub-directories to somewhere else.
    5. I need to be able to access the names of the files and their locations when they are all finally sorted to be used in writing html code. I.E. somehow pass the paths as a string when using fprintf, etc.

    I have no idea where to even begin so ANY guidance would be much appreciated. This is a pretty big project for me to be trying to undertake, which I realize.

    Thinks I have already read:
    FAQ > Accessing a directory and all the files within it - Cprogramming.com
    FindFirstFile function
    FindNextFile function
    file management in c
    Help with renaming multiple files
    As well as numerous other threads here on the boards.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your reading can't have been nearly as extensive as you claim. Most of the documentation on FindFirstFile() and FindNextFile() - including simple links you gave - give simple code samples of their usage.

    Call FindFirstFile() first, then FindNextFile() in a loop, to step through the list of files in a directory, one at a time. If you want a list of filenames, add the filename to a list. Remember to call FindClose() when the loop is done. Bear in mind that second argument (to both FindFirstFile() and FindNextFile()) is the address of a data structure. The filename the functions place in that structure is typically relative to the directory being walked through. To get a fully qualified file name (path and filename, appended) you need to append the filename and an intervening (back)slash to the path name.

    If you look up the GetFileAttributes() function, you will find it provides a way to test if a fully qualified filename is for a directory. If you are running through a directory using FindFirstFile()/FindNextFile(), consider what you need to do if you encounter a directory. Recursion provides an obvious method here.

    If you want to rename or move files (renaming is often just a move, that just happens to be on the same device) look up win32 API functions like MoveFile() and CopyFile().

    I've seen people do all five things you describe, and more, in much less than 50 lines of code. That is not what I call a "big project", by any means. I'm assuming your program doesn't need to handle slightly more advanced things, like symbolic links to files, but information on those are also easy to find if needed.
    Last edited by grumpy; 03-03-2012 at 04:59 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sprintf and tons of warnings with types - how to remove
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 07-21-2011, 03:28 PM
  2. File/Directory manipulation
    By MTK in forum C Programming
    Replies: 8
    Last Post: 05-28-2010, 10:39 AM
  3. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  4. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  5. Float/double compiler error and TONS of questions!
    By musayume in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 01:40 PM

Tags for this Thread