Write a C program to list all files in a directory recursively.
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define MAX 256
void listFiles(char *dname) {
DIR *dir;
struct dirent *ent;
char temp[MAX];
/* open the given directory */
dir = opendir(dname);
if (!dir) {
return;
}
/* list all files in the given directory directory */
printf("Files in the directory - %s:\n", dname);
while (ent = readdir(dir)) {
printf("%s\n", ent->d_name);
if (strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") ==0) {
continue;
}
/* form directory name relative to current directory */
strcpy(temp, dname);
strcat(temp, "/");
strcat(temp, ent->d_name);
/* recursive call to recursively list all files */
listFiles(temp);
}
#include <dirent.h>
#include <string.h>
#define MAX 256
void listFiles(char *dname) {
DIR *dir;
struct dirent *ent;
char temp[MAX];
/* open the given directory */
dir = opendir(dname);
if (!dir) {
return;
}
/* list all files in the given directory directory */
printf("Files in the directory - %s:\n", dname);
while (ent = readdir(dir)) {
printf("%s\n", ent->d_name);
if (strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") ==0) {
continue;
}
/* form directory name relative to current directory */
strcpy(temp, dname);
strcat(temp, "/");
strcat(temp, ent->d_name);
/* recursive call to recursively list all files */
listFiles(temp);
}
/* close the opened directory */
closedir(dir);
return;
}
int main() {
char dname[MAX];
/* get the file name from the user */
printf("Enter your directory name:");
fgets(dname, MAX, stdin);
dname[strlen(dname) - 1] = '\0';
/* lists all files in the given directory recursively */
listFiles(dname);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ls -aR mydir
mydir:
. .. data.txt editMe.txt output.txt sub1 sub2 sub3
mydir/sub1:
. .. file1.txt file2.txt file3.txt
mydir/sub2:
. .. file4.txt file5.txt
mydir/sub3:
. .. file6.txt file7.txt sub4
mydir/sub3/sub4:
. .. file8.txt file9.txt
jp@jp-VirtualBox:~/$ ./a.out
Enter your directory name:mydir
Files in the directory - mydir:
editMe.txt
data.txt
output.txt
sub1
Files in the directory - mydir/sub1:
.
..
file1.txt
file3.txt
file2.txt
sub2
Files in the directory - mydir/sub2:
.
..
file5.txt
file4.txt
sub3
Files in the directory - mydir/sub3:
.
..
file7.txt
file6.txt
sub4
Files in the directory - mydir/sub3/sub4:
.
..
mydir:
. .. data.txt editMe.txt output.txt sub1 sub2 sub3
mydir/sub1:
. .. file1.txt file2.txt file3.txt
mydir/sub2:
. .. file4.txt file5.txt
mydir/sub3:
. .. file6.txt file7.txt sub4
mydir/sub3/sub4:
. .. file8.txt file9.txt
jp@jp-VirtualBox:~/$ ./a.out
Enter your directory name:mydir
Files in the directory - mydir:
editMe.txt
data.txt
output.txt
sub1
Files in the directory - mydir/sub1:
.
..
file1.txt
file3.txt
file2.txt
sub2
Files in the directory - mydir/sub2:
.
..
file5.txt
file4.txt
sub3
Files in the directory - mydir/sub3:
.
..
file7.txt
file6.txt
sub4
Files in the directory - mydir/sub3/sub4:
.
..
file8.txt
file9.txt
SEE ALSO
- c program to write a string into a file
- c program to read numbers from a file and write even, odd and prime numbers in separate files
- c program to create a file and store "hello world" in it
- c program to read the contents of the given file
- c program to delete all blank lines in a file
- c program to check whether a directory exists or not
- c program to check whether a file exists or not
- c program to copy a file from one location to another
- c program to create, read, edit and close a file
- c program to create a file with input content
- c program to read a file line by line
- c program to compare two files character by character
- c program to concatenate two files
- c program to convert lowercase characters in a file to uppercase
- c program to convert uppercase characters in a file to lowercase
- c program to list all files in a directory
- c program to list all files in a directory recursively
- c program to append data into a file
- c program to count number of lines in a file
- c program to delete a file or directory
- c program to print the source code of itself as output
- c program to convert the contents in a file from lowercase to uppercase and vice versa
- c program to merge two files
- c program to move a file to different location
- c program to replace articles with space in a text file
- c program to print the words in a file starting with the given character
- c program to delete given word in a file
- c program to replace a word in a file
- c program to merge alternate lines from two files
- c program to remove numbers in a file
- c program to check end of file
- c program to compare two files line by line
- c program to delete specific line from a file
- c program to replace specific line in a file
- c program to take input from a file
- c program to delete a record from a file
- c program to find the number of character, words and lines in a file
- c program to sort characters in each words of a file
- c program to convert text file to binary
- c program to split a file to multiple files
- c program to find the file type, permission, size and last modification date of the given file
- c program to encrypt and decrypt contents of a file
- c program to eliminate comments from a file
No comments:
Post a Comment