Results 1 to 9 of 9

Thread: [RESOLVED] How do get information on a file?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] How do get information on a file?

    Hey guys I have a label2, is there a way to get the data a file was put on the computer for the labels caption, the file is in the same folder as the app.

    Thanks!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How do get information on a file?

    You mean the date of a file?

    Use the FileDateTime function to determine the date and time a file was created or last modified. The format of the date and time displayed is based on the locale settings of your system.

    Label2 = FileDateTime("thefile.abc")

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How do get information on a file?

    Thanks, BTW could to you tell me how to get the size of the file in KB?

  4. #4
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: How do get information on a file?

    Files size

    Add a reference to the Microsoft Scripting Runtime.

    Dim o As New FileSystemObject
    Dim oFile As File

    Set oFile = o.GetFile("c:\test.text")
    MsgBox oFile.Size

    or this way

    MyLen = FileLen("C:\autoexec.bat")
    MyDate = FileDateTime("C:\autoexec.bat")

    or using api

    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const FO_DELETE = &H3

    Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long

    Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

    Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long

    Private Declare Function CreateFile Lib "kernel32"
    Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    ------------------------------------


    this the code:

    Dim lngHandle As Long

    lngHandle = CreateFile("C:\Divi.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)


    MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."

    This will return the size of the file in Bytes
    you can do conversion from Bytes to kb
    Bytes = Bytes\1024 = kb

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How do get information on a file?

    Another way might be to open the file and use the Lof function
    Code:
    Dim intFile As Integer
    Dim sngLen As Single
    intFile = FreeFile
    Open "C:\Divi.txt" For Input As intFile
    sngLen = CSng(Lof(intFile)) / CSng(1024)
    Close intFile
    Msgbox "File size is " & Format(sngLen, "####.00) & "Kb"
    EDIT: I should point out that this will return the actual size of the file not necessarily the amount of disk space it occupies.

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How do get information on a file?

    Quote Originally Posted by Justin M
    Thanks, BTW could to you tell me how to get the size of the file in KB?
    Code:
    Label2 = Format(FileLen("thefile.abc") / 1024, "#,### KB")

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do get information on a file?

    Another alternative is simply to open its very own property page
    vb Code:
    1. Private Type SHELLEXECUTEINFO
    2.     cbSize        As Long
    3.     fMask         As Long
    4.     hwnd          As Long
    5.     lpVerb        As String
    6.     lpFile        As String
    7.     lpParameters  As String
    8.     lpDirectory   As String
    9.     nShow         As Long
    10.     hInstApp      As Long
    11.     lpIDList      As Long     'Optional parameter
    12.     lpClass       As String   'Optional parameter
    13.     hkeyClass     As Long     'Optional parameter
    14.     dwHotKey      As Long     'Optional parameter
    15.     hIcon         As Long     'Optional parameter
    16.     hProcess      As Long     'Optional parameter
    17. End Type
    18.  
    19. Private Const SEE_MASK_INVOKEIDLIST = &HC
    20. Private Const SEE_MASK_NOCLOSEPROCESS = &H40
    21. Private Const SEE_MASK_FLAG_NO_UI = &H400
    22.  
    23. Private Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long
    24.  
    25. Private Sub ShowProperties(filename As String, OwnerhWnd As Long)
    26.  
    27.   'open a file properties property page for
    28.   'specified file if return value
    29.    Dim SEI As SHELLEXECUTEINFO
    30.   'Fill in the SHELLEXECUTEINFO structure
    31.    With SEI
    32.       .cbSize = Len(SEI)
    33.       .fMask = SEE_MASK_NOCLOSEPROCESS Or _
    34.                SEE_MASK_INVOKEIDLIST Or _
    35.                SEE_MASK_FLAG_NO_UI
    36.       .hwnd = OwnerhWnd
    37.       .lpVerb = "properties"
    38.       .lpFile = filename
    39.       .lpParameters = vbNullChar
    40.       .lpDirectory = vbNullChar
    41.       .nShow = 0
    42.       .hInstApp = 0
    43.       .lpIDList = 0
    44.    End With
    45.  
    46.   'call the API to display the property sheet
    47.    Call ShellExecuteEx(SEI)
    48.  
    49. End Sub
    50.  
    51. 'Usage:   Place the full path and file name in the text box
    52. Private Sub Command1_Click()
    53. Call ShowProperties((Text1.Text), Me.hwnd)
    54. End Sub

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How do get information on a file?

    thank you!

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] How do get information on a file?

    Oh instead of making a new thread since its pretty close to the same question..

    I have a file on a website, and by using this code ..

    Label2 = FileDateTime("thefile.abc")

    How do I find the date of a file on a website? Thansk!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width