Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hidden files and FileSystemObject

Status
Not open for further replies.

miklukho

Technical User
Mar 15, 2021
4
US
Hello All,

I have a question about using FileSystemObject: when I try to go through the files in the folder, which I submit to FileSystemObject, it appears that hidden files created by Office applications - the ones with "~" at the front of file names - are being ignored so, is that true and how to overcome this as I do need these files to be processed?

Thank you
 
Would that be of any help: thread329-201352

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Hello Andrzejek,

Thank you for your reply. Sorry it took me so long to check back in.
Turns out that the problem was not that the hidden files "were ignored" - they were just processed not in order they appeared in the File Explorer and I was too quick to post my question.
But that brings another question: how to make the script go through the files and the folders in order they appear there? Any way to maintain this order during processing?

Thank you
 
Well - that depends ... what sort order do you have set in Explorer for the folder in question?
 
Hello strongm,

It is sorted by Name ascending
The whole point of the exercise is to go recursively through the entire nested folder structure inside the top-level folder and rename all the files there, using a specific naming convention - a prefix and a numeric part padded with zeros to some number of digits. I noticed that files in the subfolder, for example, "gps" get "numbered" before files in the subfolder "emails" hence, my question

Thank you
 
Ok, so, the FileSystemObject has no sorting capability

What you have to do is use it to recursively walk the directory tree, take a record of every file you find and sort them yourself ...

A quick search ion this forum should find plenty of examples of recursive directory scans
And plenty of examples of alphasorts via e.g. arrays (or even listboxes)

For example (here leveraging a .net system collection ArrayList)

Code:
[COLOR=blue]Option Explicit
[COLOR=green]' Add a reference to mscorlib.dll library[/color]

Private fso As New FileSystemObject

Private Function GetFiles(strFolder As String, Optional IncludeFolderNames As Boolean = True, Optional GetSubfolders As Boolean = False) As Object
                
    Dim myFolder  As Scripting.Folder
    Dim myFile    As Scripting.File
    
    Static Sorter As New ArrayList
     
    [COLOR=green]' process files in the folder[/color]
    For Each myFile In fso.GetFolder(strFolder).Files
        Sorter.Add myFile.Path
    Next
    
    For Each myFolder In fso.GetFolder(strFolder).Subfolders
        If IncludeFolderNames Then
             Sorter.Add myFolder.Path
        End If
        If GetSubfolders Then
            GetFiles myFolder.Path, GetSubfolders
        End If
    Next

    Set GetFiles = Sorter
End Function


Public Sub test()
    Dim Results As Variant
    Dim myList As ArrayList
    Set myList = GetFiles("d:\test\", True) [COLOR=green]' no recursion for this example[/color]
    myList.Sort ' sort the list
    
    Results = myList.toarray
   [COLOR=green] ' Results is now an alphasorted VBA array
    ' Process array however you now want[/color]
End Sub[/color]
 
Hello strongm,
Thank you for the advice and the code.
I will try

Have a great day
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top