Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here's my program for files listing:
- #include <stdio.h>
- #include <string.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <sys/stat.h>
- int list_files(char *parentdir);
- int main()
- {
- char s[80];
- system("chcp 1251");
- list_files("C:\\Documents and Settings\\user\\Desktop\\New Folder");
- system("pause");
- return 0;
- }
- int list_files(char *parentdir)
- {
- int file_count = 0;
- DIR *dir;
- struct dirent *de = NULL;
- char subdirs[1000][1000];
- int isubdirs = 0;
- char rootdir[1000];
- strcpy(rootdir, parentdir);
- FILE *fp = stdout;
- seek:
- dir = opendir(rootdir);
- if (!dir) {
- printf("\nERROR: [ %s ]\n", rootdir);
- perror("Couldn't opendir: ");
- }
- else {
- de = readdir(dir);
- if (!de) {
- printf("\nERROR:[ %s ]\n", rootdir);
- perror("Couldn't readdir ");
- goto out;
- }
- if (strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, ".") != 0) {
- fprintf(fp, "%s\\%s\n", rootdir, de->d_name);
- file_count++;
- }
- while ((de = readdir(dir)) != NULL)
- {
- if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
- continue;
- fprintf(fp, "%s\\%s\n", rootdir, de->d_name);
- int c = 0;
- char *t = de->d_name;
- file_count++;
- char filepath[1000];
- strcpy(filepath, rootdir);
- strcat(filepath, "\\");
- strcat(filepath, de->d_name);
- struct stat s;
- if (stat(filepath, &s) != 0) {
- printf("%s: ", filepath);
- perror("ERROR: stat:");
- }
- if (S_ISDIR(s.st_mode))
- {
- strcpy(subdirs[isubdirs], rootdir);
- strcat(subdirs[isubdirs], "\\");
- strcat(subdirs[isubdirs], de->d_name);
- isubdirs++;
- }
- }
- closedir(dir);
- if (isubdirs > 0) {
- strcpy(rootdir, subdirs[--isubdirs]);
- goto seek;
- }
- }
- out:
- fclose(fp);
- return file_count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement