Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get a file's modified date/time and size in bytes?
#35
this is what i did:

save this as getlastmodtimegpt.h in the same directory as qb64pe executable.

Code: (Select All)
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#endif

void get_last_modified_time_gpt(const char *filepath, char *stringret) {
#ifdef _WIN32
    WIN32_FILE_ATTRIBUTE_DATA fileInfo;
    if (GetFileAttributesEx(filepath, GetFileExInfoStandard, &fileInfo)) {
        FILETIME ft = fileInfo.ftLastWriteTime;
        SYSTEMTIME st;
        FileTimeToSystemTime(&ft, &st);
        sprintf(stringret, "%02d/%02d/%d %02d:%02d:%02d",
              st.wDay, st.wMonth, st.wYear,
              st.wHour, st.wMinute, st.wSecond);
    } else {
        sprintf(stringret, "%s", "fail");
    }
#else
    struct stat attr;
    if (stat(filepath, &attr) == 0) {
        struct tm *tm_info = localtime(&attr.st_mtime);
        char buffer[30];
        strftime(buffer, 30, "%Y-%m-%d %H:%M:%S", tm_info);
        sprintf(stringret, "%s", buffer);
    } else {
        sprintf(stringret, "%s", "stat");
    }
#endif
}

then run this program:

Code: (Select All)
declare library "getlastmodtimegpt"
    sub get_last_modified_time alias "get_last_modified_time_gpt" (afile as string, sret as string)
end declare

dim myfile$, myfiletime$
'below change it to an actual file in your system:
$IF WIN THEN
myfile$ = environ$("USERPROFILE") + "\Documents\somefile.txt"
$ELSE
myfile$ = environ$("HOME") + "/Documents/somefile.txt"
$END IF
myfiletime$ = space$(30)
get_last_modified_time myfile$, myfiletime$
print "File: "; myfile$
print "Its last modification time: "; myfiletime$
print "END RUN."
end

it came from this post:

https://qb64phoenix.com/forum/showthread...5#pid25455


i should have explained it like that before.  i don't want any enemies in this place.
Reply


Messages In This Thread
RE: how to get a file's modified date/time and size in bytes? - by hsiangch_ong - 05-05-2025, 06:16 AM



Users browsing this thread: 7 Guest(s)