Created
May 20, 2013 07:07
-
-
Save arafatkamaal/5610806 to your computer and use it in GitHub Desktop.
List files and deleted a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<dirent.h> | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<sys/types.h> | |
#include<sys/stat.h> | |
#define ERROR_OPENING_DIRECTORY -1 | |
#define SUCCESS_OPENING_DIRECTORY 1 | |
#define MAX_ABSOLUTE_PATH_LEN 1000 | |
int g_ListFilesError; | |
char** ListFiles(char* Directory,int FileCount) | |
{ | |
if(!Directory) | |
return; | |
if(0 == FileCount) | |
return; | |
DIR *dir; | |
struct dirent *directory; | |
struct stat st; | |
char **Files; | |
Files = malloc(FileCount * sizeof(char*)); | |
int Count = 0; | |
//Open a directory handle | |
dir = opendir(Directory); | |
//Neglect . and .. | |
readdir(dir); | |
readdir(dir); | |
if(dir) | |
{ | |
while((directory = readdir(dir)) != NULL) | |
{ | |
char FullPath[MAX_ABSOLUTE_PATH_LEN]; | |
strcpy(FullPath,Directory); | |
strcat(FullPath,directory->d_name); | |
stat(FullPath,&st); | |
if(S_ISDIR(st.st_mode)) | |
{ | |
continue; //Since its a directory | |
} | |
else | |
{ | |
Files[Count] = malloc(strlen(FullPath) * sizeof(char)); | |
strcpy(Files[Count],FullPath); | |
Count++; | |
} | |
} | |
closedir(dir); | |
g_ListFilesError = SUCCESS_OPENING_DIRECTORY; | |
} | |
else | |
{ | |
g_ListFilesError = ERROR_OPENING_DIRECTORY; | |
} | |
return Files; | |
} | |
int FindFileCount(char* Directory) | |
{ | |
if(!Directory) | |
return; | |
DIR *dir; | |
struct dirent *directory; | |
struct stat st; | |
int FileCount; | |
//Open a directory handle | |
dir = opendir(Directory); | |
//Neglect . and .. | |
readdir(dir); | |
readdir(dir); | |
if(dir) | |
{ | |
while((directory = readdir(dir)) != NULL) | |
{ | |
char FullPath[MAX_ABSOLUTE_PATH_LEN]; | |
strcpy(FullPath,Directory); | |
strcat(FullPath,directory->d_name); | |
stat(FullPath,&st); | |
if(S_ISDIR(st.st_mode)) | |
{ | |
continue; //Since its a directory | |
} | |
else | |
{ | |
FileCount++; | |
} | |
} | |
} | |
else | |
{ | |
return ERROR_OPENING_DIRECTORY; | |
} | |
return FileCount; | |
} | |
int RemoveFile(char* FilePath) | |
{ | |
if(FilePath) | |
{ | |
int Status = unlink(FilePath); | |
return Status; | |
} | |
} | |
int main() | |
{ | |
char* Directory = "/home/mkamaal/WorkArea/C-Programming/TestDirectory/"; | |
int FileCount = FindFileCount(Directory); | |
char** Files = ListFiles(Directory,FileCount); | |
int i = 0; | |
printf("\nNow printing files .... before deletion"); | |
for(i = 0; i < FileCount ; i++) | |
{ | |
printf("\nThe file name is %s",Files[i]); | |
} | |
RemoveFile("/home/mkamaal/WorkArea/C-Programming/TestDirectory/a"); | |
Directory = "/home/mkamaal/WorkArea/C-Programming/TestDirectory/"; | |
FileCount = FindFileCount(Directory); | |
char** Files1 = ListFiles(Directory,FileCount); | |
printf("\nNow printing files .... after deletion"); | |
for(i = 0; i < FileCount ; i++) | |
{ | |
printf("\nThe file name is %s",Files1[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment