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?
#21
(05-24-2024, 08:46 AM)euklides Wrote: In Visual Basic (Excel, Word... MSOffice) you have, for this job:

MySize = FileLen("TESTFILE") 
MyStamp$ = FileDateTime("TESTFILE")    ' Returns ... "18/10/2019 11:55:27". 

Perhaps one day theese commands will exist in QB64 Big Grin
I agree that these would come in handy, especially if cross-platform...
Reply
#22
(05-22-2024, 02:37 PM)SMcNeill Wrote: wmic DataFile where "Name='D:\\Path\\To\\myfile.txt'" get LastModified /VALUE
I tried this and it wasn't recognized. Is this something that has to be installed? 
I googled wmic and got this:
Quote:WMIC is a deprecated command-line tool in Windows that serves as an interface for Windows Management Instrumentation (WMI).
It allows you to retrieve and manage information about your computer system via WMI.
While WMI itself is still functional, WMIC is gradually being replaced by PowerShell cmdlets.
I figured out how to do it in PowerShell (code below) but both that and WMIC seem to be Windows-specific, 
and it makes me curious, is there a platform-independent way to get a file's modified date? 
I would think that getting a file's attributes would be something we'd all want on any platform? 

Code: (Select All)
_Title "Get file size + modified date"

Dim Shared m_ProgramPath$: m_ProgramPath$ = Left$(Command$(0), _InStrRev(Command$(0), "\"))
Dim Shared m_ProgramName$: m_ProgramName$ = Mid$(Command$(0), _InStrRev(Command$(0), "\") + 1)
Dim FileIn$
Dim FileOut$
Dim cmd$
Dim temp$
Dim raw$
Dim ModifiedDate$
Dim FileSizeBytes&&

Do
    ' SELECT FILE
    FileIn$ = _OpenFileDialog$("Select file to get modified date for or Cancel to quit", _CWD$, "*.bas|*.txt", "text files", 0)
    If Len(FileIn$) > 0 Then
        ' GET SIZE IN BYTES
        ' per mdijkens at https://qb64phoenix.com/forum/showthread.php?tid=2724&pid=25437#pid25437
        Open FileIn$ For Input Access Read As #99
        FileSizeBytes&& = LOF(99)
        Close #99

        ' GET MODIFIED DATE
        FileOut$ = m_ProgramPath$ + m_ProgramName$ + ".TEMP.txt"
        cmd$ = "(Get-Item '{f}').LastWriteTime.ToString('MM-dd-yyyy_HH:mm:ss.fff') | Out-File '{o}'"
        'cmd$ = "(Get-Item '{f}').LastWriteTime.ToString('yyyy-MM-dd-HH-mm-ss-fff') | Out-File '{o}'"
        cmd$ = Replace$(cmd$, "{f}", FileIn$)
        cmd$ = Replace$(cmd$, "{o}", FileOut$)
        out$ = "Powershell -Command " + Chr$(34) + cmd$ + Chr$(34)
        Shell _Hide out$
        _Delay .5
        'WHY DOES THIS RETURN A BUNCH OF JUNK WITH THE FILE?: raw$ = _ReadFile$(FileIn$)
        raw$ = ""
        Open FileOut$ For Input As #1
        Do
            Line Input #1, temp$
            raw$ = raw$ + temp$ '+ chr$(13)
        Loop Until EOF(1)
        Close
        ModifiedDate$ = RemoveUnwantedChars$(raw$, "1234567890-:._")
        'ModifiedDate$ = RemoveUnwantedChars$(raw$, "1234567890-")
        ModifiedDate$ = Replace$(ModifiedDate$, "_", " ")

        ' SHOW RESULTS
        Print "File         : " + Chr$(34) + FileIn$ + Chr$(34)
        Print "Bytes        : " + _ToStr$(FileSizeBytes&&)
        Print "Modified Date: " + Chr$(34) + ModifiedDate$ + Chr$(34)
        Print

        ' PROMPT USER
        Do
            Input "Try again (y/n)"; text$
            text$ = LCase$(Left$(_Trim$(text$), 1))
        Loop Until text$ = "y" Or text$ = "n"
        If text$ = "n" Then Exit Do
    End If
Loop

System

' /////////////////////////////////////////////////////////////////////////////

Function RemoveUnwantedChars$ (MyString$, KeepChars$)
    Dim MyResult$
    Dim iLoop%
    Dim sChar$
    MyResult$ = ""
    For iLoop% = 1 To Len(MyString$)
        sChar$ = Mid$(MyString$, iLoop%, 1)
        If InStr(1, KeepChars$, sChar$) > 0 Then
            MyResult$ = MyResult$ + sChar$
        End If
    Next iLoop%
    RemoveUnwantedChars$ = MyResult$
End Function ' RemoveUnwantedChars$

' /////////////////////////////////////////////////////////////////////////////
' FROM: String Manipulation
' found at abandoned, outdated and now likely malicious qb64 dot net website
' http://www.qb64.[net]/forum/index_topic_5964-0/
'
'SUMMARY:
'   Purpose:  A library of custom functions that transform strings.
'   Author:   Dustinian Camburides (dustinian@gmail.com)
'   Platform: QB64 (www.qb64.org)
'   Revision: 1.6
'   Updated:  5/28/2012

'SUMMARY:
'[Replace$] replaces all instances of the [Find] sub-string with the [Add] sub-string within the [Text] string.
'INPUT:
'Text: The input string; the text that's being manipulated.
'Find: The specified sub-string; the string sought within the [Text] string.
'Add: The sub-string that's being added to the [Text] string.

' Usage:
' NewString$ = Replace$(OldString$, FindMe$, ReplaceWith$)

Function Replace$ (Text1 As String, Find1 As String, Add1 As String)
    ' VARIABLES:
    Dim Text2 As String
    Dim Find2 As String
    Dim Add2 As String
    Dim lngLocation As Long ' The address of the [Find] substring within the [Text] string.
    Dim strBefore As String ' The characters before the string to be replaced.
    Dim strAfter As String ' The characters after the string to be replaced.

    ' INITIALIZE:
    ' MAKE COPIESSO THE ORIGINAL IS NOT MODIFIED (LIKE ByVal IN VBA)
    Text2 = Text1
    Find2 = Find1
    Add2 = Add1

    lngLocation = InStr(1, Text2, Find2)

    ' PROCESSING:
    ' While [Find2] appears in [Text2]...
    While lngLocation
        ' Extract all Text2 before the [Find2] substring:
        strBefore = Left$(Text2, lngLocation - 1)

        ' Extract all text after the [Find2] substring:
        strAfter = Right$(Text2, ((Len(Text2) - (lngLocation + Len(Find2) - 1))))

        ' Return the substring:
        Text2 = strBefore + Add2 + strAfter

        ' Locate the next instance of [Find2]:
        lngLocation = InStr(1, Text2, Find2)

        ' Next instance of [Find2]...
    Wend

    ' OUTPUT:
    Replace$ = Text2
End Function ' Replace$
Reply
#23
thank you to @spriggsyspriggs and gpt.

i just tried it.  it works.  i took only the example function.  i turned it into a sub that takes two string parameters.  one constant and the other which has to be allocated before it's called.  then always wrote into the return string with "sprintf" function from c.  for windows just write "fail" as return value if it failed.  for systems other than windows just write "stat" as return value if it failed.

it's funny that a solution is provided with a function microsoft had obsoleted.  also not using _readfile$ to read back the temporary file.  even though it's a pain after that to use instr on the acquired file contents to home in on the desired value.
Reply
#24
(05-02-2025, 05:10 PM)madscijr Wrote: I figured out how to do it in PowerShell (code below) but both that and WMIC seem to be Windows-specific, 
and it makes me curious, is there a platform-independent way to get a file's modified date? 
I would think that getting a file's attributes would be something we'd all want on any platform? 

the problem is that it was ridiculously easy to get the date and time of anything.  to change it at will.  for the sake of some shareware programs to use them longer.  on ms-dos and on earlier versions of windows.

but with this need for security.  for this need to timestamp packages before they are installed.  and other reasons.  things began being restricted with doing things with the date and time.  i intensely dislike on linux, being asked for my password when i have to change the date and time.  now i'm on a computer where i need to replace the cmos chip.  so i have to do one of two things after i turn on.  change the date and time manually.  or do a kludge to get it from internet.  because "ntpdig" in one of my systems is broken.  i don't want to connect to internet before turning on the computer and risking "systemd" doing something i didn't authorize it to do.  so i have this peeve of mine attacking me regularly.

therefore, there will not be a "platform independent" way of doing things with the date and time of files.  deal with it.

besides, a solution was just offered.  at least for the top three platforms.  might have to change it a little for freebsd.  or solaris.  or reactos.  or redoxos.  or other weird thing like that.


Quote:
Code: (Select All)
        'WHY DOES THIS RETURN A BUNCH OF JUNK WITH THE FILE?: raw$ = _ReadFile$(FileIn$)
this has to be a missing chr$(0) somewhere.
Reply
#25
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..


Attached Files
.doc   WHEREIS.DOC (Size: 13.72 KB / Downloads: 5)
.bas   WHEREIS.BAS (Size: 64.63 KB / Downloads: 5)
Reply
#26
(05-03-2025, 01:59 AM)hsiangch_ong Wrote: thank you to @spriggsyspriggs and gpt.

i just tried it.  it works.  i took only the example function.  i turned it into a sub that takes two string parameters.  one constant and the other which has to be allocated before it's called.  then always wrote into the return string with "sprintf" function from c.  for windows just write "fail" as return value if it failed.  for systems other than windows just write "stat" as return value if it failed.

it's funny that a solution is provided with a function microsoft had obsoleted.  also not using _readfile$ to read back the temporary file.  even though it's a pain after that to use instr on the acquired file contents to home in on the desired value.
You're telling me that to get a file's modified date from BASIC we have to do it in C? 
That makes no sense. I can do it from vbscript, VBA, PowerShell. I shouldn't have to resort to C to retrieve a file modified date.
Reply
#27
(05-03-2025, 03:51 AM)madscijr Wrote:
(05-03-2025, 01:59 AM)hsiangch_ong Wrote: thank you to @spriggsyspriggs and gpt.

i just tried it.  it works.  i took only the example function.  i turned it into a sub that takes two string parameters.  one constant and the other which has to be allocated before it's called.  then always wrote into the return string with "sprintf" function from c.  for windows just write "fail" as return value if it failed.  for systems other than windows just write "stat" as return value if it failed.

it's funny that a solution is provided with a function microsoft had obsoleted.  also not using _readfile$ to read back the temporary file.  even though it's a pain after that to use instr on the acquired file contents to home in on the desired value.
You're telling me that to get a file's modified date from BASIC we have to do it in C? 
That makes no sense. I can do it from vbscript, VBA, PowerShell. I shouldn't have to resort to C to retrieve a file modified date.
You don't need 'C' to get the file modified date/time stamp if you use the Whereis utility which I posted above..
Reply
#28
Ah, ok, sorry it was late. Nice work, it uses API functions which is how you do it in VBA.
Reply
#29
(05-03-2025, 02:04 AM)hsiangch_ong Wrote: the problem is that it was ridiculously easy to get the date and time of anything.  to change it at will.

I get that they wouldn't want it to be easy to CHANGE a file's timestamp, but that's different from READING a timestamp. Tightening security 'til you can't even use your computer for the things it should be able to do drives me crazy too. That said, I can't imagine it would be impossible to READ a file's modified date cross-platform, it just would use different methods under the hood depending on the platform.

(05-03-2025, 02:04 AM)hsiangch_ong Wrote:
Quote:
Code: (Select All)
        'WHY DOES THIS RETURN A BUNCH OF JUNK WITH THE FILE?: raw$ = _ReadFile$(FileIn$)
this has to be a missing chr$(0) somewhere.

Do you mean missing in the temp file being read?
Reply
#30
(05-03-2025, 03:51 AM)madscijr Wrote: You're telling me that to get a file's modified date from BASIC we have to do it in C? 
That makes no sense. I can do it from vbscript, VBA, PowerShell. I shouldn't have to resort to C to retrieve a file modified date.

(05-03-2025, 04:10 AM)eoredson Wrote: You don't need 'C' to get the file modified date/time stamp if you use the Whereis utility which I posted above..

do it how you guys want.  because it will work only on windows.  with utilities that could be used on windows.  oh well but now we have wine on linux and other unix descendants.  pardon me.

in my post (out of spriggsyspriggs' efforts and others) i was only suggesting a very close way to have a function.  which is multi-platform.  and feels like it's part of qb64.  where does it say we could use another basic to supplement qb64?  or we could use powershell, heaven forbid?  powershell was one of the great banes of my interest in windows10.  because frecking file explorer could only open that.  and not "cmd.exe" which i needed much more.

especially away from windows.  functionality for other languages such as lua, python and whatever was always done with c.  if not with c++.  there isn't an application anymore written entirely in one language or another.  not even qb64pe could claim to be "purely" in basic.  or in c++.

i didn't want to answer like this.  but your comment left me too surprised to post yesterday.  i didn't know what to say, and how to say it.
Reply




Users browsing this thread: mdijkens, 3 Guest(s)