File manipulation with VBA

Posted on 2007-11-30
Last Modified: 2008-02-01

I would like to be able to change file attributes using vba. Also, I would like to see creation date and last accessed date.

Here's an example of what I'd like to do with my code:
1) Change attribute of a file to hidden, read-only, or archive
2) Get the last access date of the file or last modified date.

I would like this code to run for all files in a folder X.
Question by:alateos
  • 3
LVL 38

Expert Comment

Comment Utility
1.  Use SetAttr
SetAttr Path, vbHidden Or vbSystem

2. Use FileDateTime, which returns date created or modified

Dim dt as Date
dt = FileDateTime(Path)

For last modified date, you should consider the FileSystemObject...
LVL 38

Expert Comment

Comment Utility
>For last modified date, you should consider the FileSystemObject...

Meant to be last access date...  FileSystemObject can get all the different types of file date/time stamps:


Dim fso, file, createdate
Set fso= CreateObject("Scripting.FileSystemObject")
Set file= fso.GetFile("C:\temp\test.txt")
msgbox "Created " &  file.DateCreated
msgbox "Modified " & file.DateLastModified
msgbox "Last accessed " & file.DateLastAccessed


Author Comment

Comment Utility
thanks for your reply Paul... could you please show me a full practical example... where the app looks through a folder... also please show how one can set the attribute in the example
LVL 38

Accepted Solution

PaulHews earned 250 total points
Comment Utility
Here's an example without the FileSystemObject using pure VB functions.  Let me know if you actually need the access date.
Public Function fGetFiles(ByVal strSpec As String, ByVal strPath As String) As Variant

    Dim strFile() As String, strTemp As String

    Dim lngCount As Long, lngNum As Long

    Const Chunk = 200

    On Error GoTo errGetFiles

    lngNum = Chunk

    ReDim strFile(1 To lngNum)

    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"

    lngCount = 1

    strTemp = Dir(strPath & strSpec)

    Do While Len(strTemp) > 0

        If lngCount > UBound(strFile) Then

            lngNum = lngNum + Chunk

            ReDim Preserve strFile(1 To lngNum)

        End If

        strFile(lngCount) = strTemp

        strTemp = Dir

        lngCount = lngCount + 1


    If lngCount > 1 Then

        ReDim Preserve strFile(1 To lngCount - 1)


        ReDim strFile(-1 To -1)   'Avoid errors when no files.  Send back a one element blank array.

    End If

    fGetFiles = strFile

    Exit Function



    MsgBox Err.Description, vbCritical, "fGetFiles"

End Function

Private Sub Command1_Click()

    Dim strFiles() As String

    Dim i As Long

    Dim strPath As String


    strPath = "C:\temp\"


    strFiles = fGetFiles("*.*", strPath)

    For i = 1 To UBound(strFiles)

        Debug.Print strFiles(i) & ": " & FileDateTime(strPath & strFiles(i))

        SetAttr strPath & strFiles(i), vbReadOnly Or vbHidden


End Sub

Open in new window


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.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

771 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

15 Experts available now in Live!

Get 1:1 Help Now