Script - Last File Accesssed

Posted on 2005-04-07
Last Modified: 2010-04-18
Hi all,

Does anybody know where I can find a batch/vb script that will print to a file all filenames & folders the were last accessed 6 months ago?

Want to run this on my file server where I've got tons of files that were used once and never again.

Question by:stevengreen
    LVL 76

    Accepted Solution

    Hi stevengreen,

    The following script should do the trick.  To use it do the following:
    1.  Start Notepad
    2.  Copy the script below and paste it into Notepad
    3.  Edit the script as needed.  You'll need to change strBasePath to point to the root of the path you want the script to run against
    4.  Save the Notepad file.  You may name it anything you want so long as the name ends with .vbs
    5.  Run the script by issuing this command from either a command prompt or the run command dialog-box:
         cscript scriptname.vbs > outputfilename

    'Script begins here
    Option Explicit
    Dim objFSO, intFolders, intFiles, strBasePath

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Set the path to the folder we'll start in
    strBasePath = "C:\"
    'Call the ProcessFolder subroutine and pass it the starting path
    ProcessFolder strBasePath
    'Clean up
    Set objFSO = Nothing
    Wscript.Echo ""
    Wscript.Echo "Folders = " & intFolders & vbtab & "Files = " & intFiles

    Private Sub ProcessFolder(strFolderPath)
        Dim objFolder, objSubFolders
        'Create the starting folder object
        Set objFolder = objFSO.GetFolder(strFolderPath)
        'Call the ProcessFiles subroutine and pass it the folder object
        ProcessFiles objFolder
        intFolders = intFolders + 1
        'Create the sub-folders object
        Set objSubFolders = objFolder.SubFolders
        'Loop through the sub-folders in the folder object
        For Each objFolder In objSubFolders
            'Call the ProcessFiles subroutine and pass it the sub-folder object
            ProcessFolder objFolder.Path
            intFolders = intFolders + 1
        'Dispose of the created objects
        Set objSubFolders = Nothing
        Set objFolder = Nothing
    End Sub

    Private Sub ProcessFiles(objFolder)
        Dim objFile
        'Loop through the files in the folder object
        For Each objFile In objFolder.Files
            If Datediff("m", objFile.DateLastAccessed, date()) >= 1 Then
                Wscript.Echo objFile.Path
                intFiles = intFiles + 1
            End If
        'Dispose of the object created in this subroutine
        Set objFile = Nothing
    End Sub
    'Script ends here

    LVL 3

    Author Comment


    This looks awesome.  Is there a way that I can get it to print the date next to each filename as well?

    Also, if I say wanted to change it from 6 months to 1 year how would I go about doing that?

    Thank you for your help....
    LVL 76

    Expert Comment

    by:David Lee
    Sure.  To show the date change this line

        Wscript.Echo objFile.Path


        Wscript.Echo objFile.Path & " " & objFile.DateLastAccessed

    To change from 6 months to 1 year, change the 6 to a 12.

    No problem.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now