getting the date from a file

Posted on 2007-10-05
Last Modified: 2013-11-26
Using the filelist box, i want to get the date of the file selected, is this possible?
Question by:markmchugh
    LVL 12

    Expert Comment

    'Recursive directory file list
    'July 2007 - Robert Heritage -
    'Please mention me in your script if you use this code :)
    ' to pipe the output into the file use: cscript thisfile.vbs > outfile.csv

    Dlm = ","' Variable Delimiter for output
    redim FolderList(0)'A flat list of the directory subtree

    FolderList(0) = "c:\"'can also use \\servername\c$ etc but it is sloooow

    'This part of the script walks the directory structure and stores all of he folder paths in the FolderList Array.

    FolderIndex = 0
    Do Until FolderIndex > Ubound(FolderList)

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(folderList(folderindex))

    If f.subfolders.count > 0 Then

    For Each folder In f.subFolders

    if folder.attributes <> 22 Then 'Exclude hidden system folders
    redim preserve FolderList(Ubound(FolderList)+1)
    FolderList(ubound(FolderList)) = folder.path
    End If
    End If
    FolderIndex = FolderIndex + 1

    'Set up a header for the output

    wscript.echo ubound(FolderList) & " Folders. Root: " &Folderlist(0)
    wscript.echo "FileName" & dlm & "Folder" & dlm & "Size" & dlm & "Date Created" & dlm & "Age" & dlm & "Date Modified" & dlm & "UnModified Age" & dlm & "Last Accessed" & dlm & "Unread Age" & dlm & "peers"

    'This part of the script Works through the list of folders and outputs the data for each file

    for n = 0 to ubound(FolderList)

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(FolderList(n))
    For Each file In f.Files
    dslc = DateDiff("d",Now,file.datecreated) * -1
    dslm = DateDiff("d",Now,file.dateLastModified) * -1
    dsla = DateDiff("d",Now,file.dateLastAccessed) * -1

    wscript.echo file.Name & dlm & file.parentfolder & dlm & file.size & dlm & file.DateCreated & dlm & dslc & dlm & file.DateLastModified & dlm & dslm & dlm & file.DateLAstAccessed & dlm & dsla & dlm & f.Files.count

    LVL 12

    Expert Comment

    This is something I found.
    LVL 65

    Expert Comment

    Hi, you can do this using the Microsoft Scripting Runtime Library, which you'll need to add a reference to in your project properties, then you can use the FileSystemObject.

    if cdlg.FileName = "" Then
        ' User canceled.
        ' The FileName property contains the selected file name.
       strFileName = cdlg.FileName
       ' This is the part that uses the Microsoft Scripting Runtime Library (VBScript code)
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       dteLastModified = objFSO.GetFile(strFileName).DateLastModified
       MsgBox strFileName & vbCrLf & "was last modified on: " & dteLastModified
    End If


    LVL 10

    Accepted Solution

    Although I'm a huge fan of the Scripting Runtime you can do this in VB itself - which if you haven't used the FSO for anything else, gives you one less dependency to worry about:

        Dim fileName As String
        fileName = File1.Path & File1.fileName

        MsgBox "fileName's date/time is " & FileDateTime(fileName)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now