0

I created documentary list in Excel 2016 for a personal project, it can sort files and remove old versions, create a clickable link to the new version... I want to update code for better reliability and visibility.

I don't how to create a dynamic list of objects with files properties (size, name, path, creation date...), ex : have these properties(size, name, path...) I add a piece of my old code.

I tried these tutorials but isn't working or I don't understand :

Dim TblFichiers() As String
Dim File As String
Dim I As Integer

If Right(Chemin, Len(Chemin)) <> "\" Then Chemin = Chemin & "\"
File = Dir(Chemin & "*" & Ext & "*")

Do While (Len(File ) > 0)
    I = I + 1
    ReDim Preserve TblFichiers(1 To I)
    TblFichiers(I) = File 
    File = Dir()
Loop

Getfiles= TblFichiers() 'Getfiles is main function

I know how to do this in C ++ or C# but I have difficulty understanding how it works in VBA.

3
  • You need to define a Class which groups the variables you want to record. You need to create new instances of the class for each record. Add the record to a Collection or Scripting.Dictionary. Scripting Dictionaries offer a number of advantages to Collections. Commented Apr 8, 2019 at 9:17
  • 1
    What’s “weight” in relation to the file? What does that attribute represent? Are you meaning size? Commented Apr 8, 2019 at 9:55
  • @Skin Yes it's size, excuse me my english is not very well Commented Apr 8, 2019 at 11:07

1 Answer 1

1

I think you'd be best to look at the Microsoft Scripting Runtime library ..

Microsoft Scripting Runtime

Public Sub TraverseFiles()
    Dim strFolder As String

    Dim objFSO As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    Dim objFile As Scripting.File

    strFolder = "c:\temp"

    Set objFSO = New Scripting.FileSystemObject
    Set objFolder = objFSO.GetFolder(strFolder)

    For Each objFile In objFolder.Files
        Debug.Print objFile.Name
        Debug.Print objFile.Path
        Debug.Print objFile.Size
        Debug.Print objFile.DateCreated
    Next
End Sub

... it'll give you what you want regarding attributes about files and operations to manage those files.

You just need to adapt it your project.

I hope that works for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! I have a last question : how i can access to attributes per files, ex : "MsgBox objFolder.Files.Item(0).Path" without For Each ?
@Bensuperpc, using a for loop doesn't work too well. For some reason using the index of the file in the collection produces an error, it's obviously just the way Microsoft have written the library. If you need some sort of index, just increment a variable through each loop, does that help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.