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?
#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


Messages In This Thread
RE: how to get a file's modified date/time and size in bytes? - by madscijr - 05-02-2025, 05:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Font Size in Threads Dimster 5 377 12-12-2025, 04:49 PM
Last Post: Dimster
  auto-detecting screen resolution for optimal window size on non-Windows systems madscijr 11 1,084 11-10-2025, 07:23 PM
Last Post: madscijr
  auto-detecting screen resolution for optimal window size on non-Windows systems madscijr 0 227 10-26-2025, 06:58 PM
Last Post: madscijr
  question on determining time spent in area fistfullofnails 9 845 08-25-2025, 01:55 PM
Last Post: fistfullofnails
  Testing against multiple elements at a time without select case CMR 4 845 04-17-2025, 02:16 AM
Last Post: CMR

Forum Jump:


Users browsing this thread: 1 Guest(s)