2

I'm trying to create a function that scans a folder on my Windows PC and every time it does, a file called "Filter.txt" is appended with the string "Test Script".

Now the problems are 2, the first is that the scan must be performed either in the directory c:\LOG or its subdirectories, and the second is that I do not know how to chain fopen in the directory and the name of the file.

int main(){
    DIR *dir;
    FILE * pFile;
    char myString[100];
    struct dirent *ent;
    dir = opendir ("c:\\LOG");
    if (dir != NULL) {
        /* print all the files and directories */
        while ((ent = readdir (dir)) != NULL) {
            pFile = fopen ("Filter.txt","a");
            if (pFile==NULL)
                perror("Error");  
            else
                fprintf(pFile,"%s\n","Test scriptIno");
            fclose(pFile);
            //printf ("%s\n", ent->d_name);
        }
        closedir (dir);
    } else {
        /* Can not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
}
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
AleMal
  • 1,977
  • 6
  • 24
  • 49
  • You shouldn't use C for such task. Look better into a scripting language like Perl. Each language/technology was developed for a certain purpose. C was developed to make operating systems, not to do scripting. If you're interested I can show you an example in Perl. – m0skit0 Nov 16 '11 at 13:04
  • 2
    @m0skit0: In K&R, they describe C as a "general-purpose language". I did not find a disclaimer like "use it for developing OSes only". – undur_gongor Nov 16 '11 at 13:23
  • @undur_gongor: that was back in 1972. Nowadays you don't want to code in C a script like this. It's just a waste of time unless you want to actually learn C. This said, C is probably my favourite language, but one has to know when to use what. – m0skit0 Nov 16 '11 at 13:26
  • @m0skit0: Not 1978? Although in general I agree, there might be reasons to do such things C. – undur_gongor Nov 16 '11 at 13:33
  • Yes, 1978 for K&R, correct. 1972 was the year C was actually born. There might be reasons, as I also stated on my previous comment, but scripting languages were introduced for a reason. – m0skit0 Nov 16 '11 at 16:44

1 Answers1

1

For how to chain calls to opendir you can find plenty of answers here on SO, for example this. Use ent->d_type to check if the entry is a directory or a file.

For opening a file in the directory, just use the pathname in ent->d_name to construct the path for the fopen call.

Edit Was a little bored at work, and made a function like the one you maybe want...

#ifdef _WIN32
# define DIR_SEPARATOR "\\"
#else
# define DIR_SEPARATOR "/"
#endif
void my_readdir(const char *path)
{
    DIR *dir = opendir(path);
    if (dir != NULL)
    {
        struct dirent *ent;

        static const char filtername[] = "filter.txt";

        /* +2: One for directory separator, one for string terminator */
        char *filename = (char *) malloc(strlen(path) + strlen(filtername) + 2);

        strcpy(filename, path);
        strcat(filename, DIR_SEPARATOR);
        strcat(filename, filtername);

        FILE *fp = fopen(filename, "a");

        while ((ent = readdir(dir)) != NULL)
        {
            if (ent->d_type == DT_REG || ent->d_type == DT_DIR)
            {
                if (strcmp(ent->d_name, "..") != 0 && strcmp(ent->d_name, ".") != 0)
                {
                    if (fp != NULL)
                        fprintf(fp, "%s : %s\n", (ent->d_type == DT_REG ? "File" : "Directory"), ent->d_name);

                    if (ent->d_type == DT_DIR)
                    {
                        /* +2: One for directory separator, one for string terminator */
                        char *newpath = (char *) malloc(strlen(path) + strlen(ent->d_name) + 2);

                        strcpy(newpath, path);
                        strcat(newpath, DIR_SEPARATOR);
                        strcat(newpath, ent->d_name);

                        /* Call myself recusively */
                        my_readdir(newpath);

                        free(newpath);
                    }
                }
            }
        }

        if (fp != NULL)
            fclose(fp);
        free(filename);
    }
}

Edit It seems that the opendir and readdir functions are not very well supported on Windows. Use the Windows-only FindFirstFile and FindNextFile in a similar fashion to my example above. See this MSDN page for an example on how to use these functions.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • if i create path like this myString = strcat(ent->d_type, "\\Filter.txt"); the compiler respond "structure has no member named `d_type' " and "incompatible types in assignment " – AleMal Nov 16 '11 at 13:18
  • @user1035523 `ent->d_type` is the type of the file, not the name. And you have to create a new string dynamically to contain all the separate parts of the path. – Some programmer dude Nov 16 '11 at 13:32
  • Thanks a lot, but when i run the function compiler respond "`DT_REG' undeclared (first use in this function) " at the line "if (ent->d_type == DT_REG || ent->d_type == DT_DIR)" – AleMal Nov 17 '11 at 06:17
  • ..I HAVE ALSO INCLUDED dirent.h bat nothing changed. Thanks – AleMal Nov 17 '11 at 06:35