WIN32 getting last modified date's of specified path

So i got it to work on unix, but i really need it to work for windows instead. The code below will get all the files in the folder and print the last modified date. How would i go about doing this for windows?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>

void getFileCreationTime(char *path)
{
    struct stat attr;
    stat(path, &attr);
    printf("Last modified time: %s", ctime(&attr.st_mtime));
}
int main(void)
{
  DIR           *d;
  struct dirent *dir;
  d = opendir("C:\\test\\"); //path
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
        //exclude
        if( strcmp( dir->d_name, "." ) == 0 ||
        strcmp( dir->d_name, ".." ) == 0 )
        {
            continue;
        }
        printf("%s\n", dir->d_name);
        getFileCreationTime(dir->d_name);
    }
    closedir(d);
  }
  return(0);
}
How would i go about doing this for windows?

I’m not able to help you with your code, but if you have the Microsoft compiler, it’s possible this code does the same thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ...get all the files in the folder and print the last modified date.
#include <chrono>
#include <filesystem>
#include <iostream>

using namespace std::experimental::filesystem::v1; // Microsoft Visual Studio

int main()
{
    for(const auto& it : directory_iterator("C:\\test\\")) {
        auto ftime = last_write_time(it);
        std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime);
        std::cout << it << " last modified time was "
                  << std::asctime(std::localtime((&cftime)))
                  << '\n';
    }
}


I worked out the above code from the examples in this page
http://en.cppreference.com/w/cpp/filesystem
and the related ones, but I can’t test it because I don’t have the Microsoft compiler (and my compiler doesn’t support “filesystem” yet).

Otherwise, if you have boost, this code works for sure (I’ve tested it):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...get all the files in the folder and print the last modified date.
#include <chrono>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
#include <iostream>

namespace bf = boost::filesystem;

int main()
{
    for(const auto& it : bf::directory_iterator("C:\\test\\")) {
        std::time_t ftime = last_write_time(it);
        std::cout << it << " last modified time was "
                  << std::asctime(std::localtime(&ftime))
                  << '\n';
    }
}

Please remember you need to link libboost_system.a and libboost_filesystem.a.
Topic archived. No new replies allowed.