-2

i am working on a home made little program that is going to make my life a lot easier.

the idea is it looks at a directory, sees the file names in it, removes all certain character combinations and certain characters from the name of the files, then copies the newly named file to a separate folder, and deletes the original.

i can do most of that. what i dont know is how to load the file names into my program i can figure everything else out as i know how to manipulate strings in C and so on.

ive been looking for an easy to implement solution for a few days and found nothing.

tldr:

look at directory load all file names change all file names based on criteria copy files to new directory.

i dont really know how to do step 1, 2, or 4.

i dont expect you guys to write the program for me, even a library and command suggestion would be great if there is one.

Saabi Atassi
  • 1
  • 1
  • 1
  • 2
    Possible duplicate of [How can I get the list of files in a directory using C or C++?](https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c) –  Apr 22 '18 at 04:53

1 Answers1

1

See a few related questions : How can I get the list of files in a directory using C or C++? Read file names from a directory

And as stated on this page : https://www.geeksforgeeks.org/c-program-list-files-sub-directories-directory/

Using the <dirent.h> module, doing so :

#include <stdio.h>
#include <dirent.h>

int main(void)
{
    struct dirent *de;  // Pointer for directory entry

    // opendir() returns a pointer of DIR type. 
    DIR *dr = opendir(".");

    if (dr == NULL)  // opendir returns NULL if couldn't open directory
    {
        printf("Could not open current directory" );
        return 0;
    }

    // Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
    // for readdir()
    while ((de = readdir(dr)) != NULL)
            printf("%s\n", de->d_name);

    closedir(dr);    
    return 0;
}

Would allow you to see the files and directories inside a directory. I think with this, you should be good.

Alceste_
  • 592
  • 4
  • 13
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19511027) – SilverNak Apr 22 '18 at 05:23
  • 1
    While this is technically true, I don't think answers specifically refering to a single technology (dirent.h in this case) would stay valid if the link pointing to that technology die. Technologies are at least as much subject to changes as the languages main reference pages. – Alceste_ Apr 22 '18 at 05:26
  • 1
    The intent had never been to critique or request clarification though, but to provide them with a solid example of how the desired behavior can be accomplished. I'll edit to include the code.. – Alceste_ Apr 22 '18 at 05:35