7

I'm trying to create a function in c which scans all my path C: \ temp (Windows) to search for a file that I pass (eg test.txt) and each time it finds one return the path to steps another function to write something in the bottom of this file. I managed to do the function that writes to the file but can not figure out how to do that scans the folder and pass the address of the file found.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
AleMal
  • 1,977
  • 6
  • 24
  • 49
  • If you also want to check file extensions then scandir () may be useful https://stackoverflow.com/questions/22886290/c-get-all-files-with-certain-extension – Sergey Ponomarev Jul 05 '20 at 20:15

2 Answers2

14
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

int main()
{
    printf("Directory scan of /home:\n");
    printdir("/home",0);
    printf("done.\n");
    exit(0);
}
kaetzacoatl
  • 1,419
  • 1
  • 19
  • 27
Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • ...but where i insert my window scan path like "c:\tools" and then the file txt that i whant find and modify like "test.txt" and than the string that i whant insert at the end of it like "Sometext"? – AleMal Nov 16 '11 at 10:22
  • 1
    pass the directory name in the function printdir(“c:/xxx/xx/”,0); Modify the code as per ur need , this code will list all items under a given directory – Akhil Thayyil Nov 16 '11 at 10:25
  • I have modify like this for find files named Filter.txt in directory LOG and write "done" at the end of file...but don't work int main(){ printf("Directory scan of /home:\n"); printdir("C:/LOG/Filter.txt",0); printf("done.\n"); exit(0);} – AleMal Nov 16 '11 at 10:39
  • int main() { printf(“Directory scan of /home:\n”); printdir("C:/LOG/Filter.txt",0); printf(“done.\n”); exit(0); } – Akhil Thayyil Nov 16 '11 at 11:38
  • 1
    Note that while `chdir(dir);` is OK, the ending `chdir("..");` is not a general solution to the problem of 'change directory back to where you came from'. It only works when the named directory is an immediate subdirectory of the current directory, I think. On POSIX systems, you can use [`fchdir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html) to change directory safely: `int cwd = open(".", O_RDONLY);` opens the current directory before you do the initial `chdir()`, then `fchdir(cwd)` changes back to that directory — followed by `close(cwd)` of course. – Jonathan Leffler Aug 01 '14 at 06:00
2

Use FindFirstFile function. Here's a good example of this function using.

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34