My goal is to try to get the name, size and last modified of a folder. I have used dirent.h to get the first two types. I don't know what the best way of getting the last modified value is, but the current way I am using does not seem very useful. I am getting type errors when trying to get my path as the dirent folder that I am opening. I'm not sure if there is an easier way to do this or but any guidance would be much appreciated. I found an example of how to get the last modified on stackoverflow and was trying to use that... below is my code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
void getFileCreationTime(char *path) {
struct stat attr;
stat(path, &attr);
printf("Last modified time: %s", ctime(&attr.st_mtime));
}
int main()
{
DIR *folder;
struct dirent *entry;
int files = 0;
folder = opendir(".");
if(folder == NULL)
{
perror("Unable to read directory");
return(1);
}
while( (entry=readdir(folder)) )
{
files++;
printf("File %3d: %s\n",files, entry->d_name);
printf("File %3d: %i\n",files, entry->d_reclen);
getFileCreationTime(folder);
}
closedir(folder);
return(0);
}
With error:
main.cpp: In function ‘int main()’:
main.cpp:36:35: error: cannot convert ‘DIR* {aka __dirstream*}’ to ‘char*’ for argument ‘1’ to ‘void getFileCreationTime(char*)’
getFileCreationTime(folder);
Again, I am trying to be able to get the last modified date of each folder that I am getting the name and size of using dirent.