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?
#31
Try this one simple command on your system:

Code: (Select All)
forfiles /C "cmd /c echo @fsize @file @fdate @ftime"

Open a console or powershell terminal, paste that in it, and see if it doesn't do what you want. If it does, then pipe the output to clipboard or a file and go from there with a simple SHELL call.

NOTE that forfiles has several options such as /P for path and such, and /S for recursive use in subdirectories, so take a bit to study its syntax and help info to customize it for exactly what you want it to do.

FORFILES [/P pathname] [/M searchmask] [/S]
        [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command      Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname  - returns the file name without
                                  extension.
                        @ext    - returns only the extension of the
                                  file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                  file.
                        @isdir  - returns "TRUE" if a file type is
                                  a directory, and "FALSE" for files.
                        @fsize  - returns the size of the file in
                                  bytes.
                        @fdate  - returns the last modified date of the
                                  file.
                        @ftime  - returns the last modified time of the
                                  file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.
Reply
#32
Thanks Steve, now we have at least 4 ways to do it in Windows - Powershell, API, forfiles and that other one. I'm hearing them say there is no easy way in Mac or Linux, though googling it says for Linux/Mac:

Code: (Select All)
stat -c "%y : %n" my_file.txt

This command will output something like:
Code

2024-05-04 10:00:00 : my_file.txt
Can that be shelled out from QB64PE on Mac / Linux?
Reply
#33
These are the commandline switches for my Whereis utility:

Note: there is switches for date/time when specified as /1 or /2 or /3:

Code: (Select All)
Whereis v1.0a: File search utility;
Usage:
  Whereis [@][d:\path\filename.ext][//ahiosx][/bcdefgjklnpqrtuvwyz@#][/123]
Where:
  /b  suppress drive letter      /c  continuous display
  /e  short filename display    /g  display search directories
  /j  skip current directory    /k  use 8.3 short filenames
  /l  double line display        /nxxx  nested directories
  /q  directory count override  /r  recurse directories
  /u  remove trailing slash      /v  display volume label
  /w  wide file list            /y  display lowercase
  /z  no error messages          /z1 display only errors
display ranges:
  /1  creation, /2  last access, /3  modify time
  /d  is range of file dates in form mm/dd/yyyy-mm/dd/yyyy
  /t  is range of file times in form hh:mm:ss-hh:mm:ss
  /f  is range of file sizes in form xxx-xxx in kilobytes
  filelist switches:
    /@  enable filelists  /#  ignore filelist prompts
  exclude file list switch:
    /(<filename.ext>)
      excludes files containing ? and * characters.
  display file attributes: // prefix for files with, / prefix for files without
    a  archive, h  hidden, i  directory, o  read-only, s  system
    m1  compressed,  m2  encrypted, x  none
  DOS command: /[<command>]
    with command replacement parameters: (use /p for /c override);
      %1 = d:, %2 = d:\, %3 = d:\pathname, %4 = d:\pathname\
      %5 = d:\pathname\filename.ext, %6 = \pathname, %7 = \pathname\
      %8 = \pathname\filename.ext, %9 = filename.ext, %a = filename
      %b = .ext, %c = ext, //1 = >, //2 = <, //3 = |, //4 = %, //5 = ]
Reply
#34
Good stuff - thanks for sharing. I will play with it when I get back to the PC!
Reply
#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
#36
(05-03-2025, 03:46 AM)eoredson Wrote: I have this program called Whereis which also displays filesize/attributes and file creation/access/modified.

It has many commandline switches for extended displays.

Erik.

Note: You must use the /L switch to get additional file information..
I also have this program called Stree which displays directories with attributes and date/time creation/access/modified.

It has many commandline switches for extended displays.

Erik.

Note: You must use the /E switch to get additional directory information.


Attached Files
.bas   STREE.BAS (Size: 34.45 KB / Downloads: 4)
.doc   STREE.DOC (Size: 3.95 KB / Downloads: 3)
Reply




Users browsing this thread: 1 Guest(s)