Results 1 to 4 of 4
  1. #1
    Travb81 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Aug 2014
    Posts
    18

    dir() directory listing - unusual short filename result returned

    Brief intro:


    We use 5 digit job numbers. Each job has a job folder starting with these digits, followed by some of it's job name.
    To locate the job folder, I search using the job number followed by a astrix wildcard.


    I'm experiencing some odd results from dir() directory listings.

    I've found that there are Short File name listings (eg 8 character name, 3 character extension), which can be returned with the "dir /x" extension.

    The weird behaviour, is that sometimes Windows has created a folder, where the short name is quite different from the long name.
    When I'm relying on a long file name result, it's giving me a weird unexpected result.

    An example to explain....
    Code:
    dir t5246*
     Directory of N:\Jobs
    17/11/2018  10:19 AM    <DIR>          T4411 - New Flooring

    Code:
    dir t5246* /x
    17/11/2018  10:19 AM    <DIR>          T52463~H     T4411 - New Flooring

    I've tried to utilise the Scripting.FileSystemObject methods instead - but they are much slower than a native DIR() command.
    I cant see a switch for the dir() command, to exclude short file name results.

    Any ideas?
    Is there a way to manipulate the dir() data more, maybe to compare the short and long filename - if they are significantly different, ignore that dir() result and look for the next one?

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    don't use DIR, instead use ("Scripting.FileSystemObject")

    Code:
    Public Function FileExists(ByVal pvFile) As Boolean
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    FileExists = fso.FileExists(pvFile)
    Set fso = Nothing
    End Function
    
    
    
    Public Function DirExists(ByVal pvDir) As Boolean
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    DirExists = fso.FolderExists(pvDir)
    Set fso = Nothing
    End Function
    

  3. #3
    Travb81 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Aug 2014
    Posts
    18
    Thanks....
    But FileSystemObject doesn't work with wildcards?
    If i use the fso.GetFolder routine, and loop through each .SubFolder result, the outcome is too slow.


    Quote Originally Posted by ranman256 View Post
    don't use DIR, instead use ("Scripting.FileSystemObject")

    Code:
    Public Function FileExists(ByVal pvFile) As Boolean
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    FileExists = fso.FileExists(pvFile)
    Set fso = Nothing
    End Function
    
    
    
    Public Function DirExists(ByVal pvDir) As Boolean
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    DirExists = fso.FolderExists(pvDir)
    Set fso = Nothing
    End Function
    

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You don't say what you are doing with the folder name(s), but I found some code (at https://www.excelhowto.com/macros/ho...use-excel-vba/ and https://excelmacromastery.com/vba-di...VBA_Dictionary ) and modified it.

    Right now the code asks you to pick a folder and loops through the path collecting all folders/sub folders. It found over 4400 folders (I picked a drive) in under 3 seconds (I didn't time it, but is was fast).
    You could modify the "Select folder" code to search for a folder, if that is what you need.
    Then it writes the found folders to the immediate window.
    Instead of writing to the immediate window, you could modify the code to write to a table, then do what you need to do with the folders.
    Code:
    Sub ListAllFolders()
    
        Dim MyPath As String, MyFolderName As String
        Dim i As Integer, F As Boolean
        Dim objShell As Object, objFolder As Object, AllFolders As Object
        Dim key
        Dim tmp()
        On Error Resume Next
    
        '************************
        'Select folder
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0, "", 0, 0)
        If Not objFolder Is Nothing Then
            MyPath = objFolder.self.Path & "\"
        Else
            Exit Sub
            'MyPath = "G:\BackUp\"
        End If
        Set objFolder = Nothing
        Set objShell = Nothing
    
        '************************
    
    
        'Find all folders/sub folders
        Set AllFolders = CreateObject("Scripting.Dictionary")
    
        AllFolders.Add (MyPath), ""
        i = 0
        Do While i < AllFolders.Count
            key = AllFolders.Keys
            MyFolderName = Dir(key(i), vbDirectory)
            Do While MyFolderName <> ""
                If MyFolderName <> "." And MyFolderName <> ".." Then
                    If (GetAttr(key(i) & MyFolderName) And vbDirectory) = vbDirectory Then
                        AllFolders.Add (key(i) & MyFolderName & "\"), ""
                    End If
                End If
                MyFolderName = Dir
            Loop
            i = i + 1
        Loop
    
    
        'display the folders found in the immediate window
        PrintContents AllFolders
    
    
        Set AllFolders = Nothing
    End Sub
    
    
    
    '##########################################
    
    Sub PrintContents(dict As Scripting.Dictionary)
        
        Dim k As Variant
        For Each k In dict.Keys
            ' Print key and value
            Debug.Print k   ' << could write to a table here
        Next
    
    End Sub

    EDIT: the folder/sub folders includes the full path, so if you want just the folder name, you will have to parse the string to get just the folder name.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2018, 02:20 PM
  2. A most unusual query
    By Lou_Reed in forum Access
    Replies: 3
    Last Post: 04-27-2017, 03:13 PM
  3. Replies: 3
    Last Post: 03-18-2016, 10:07 AM
  4. Replies: 6
    Last Post: 10-26-2012, 12:53 PM
  5. Replies: 6
    Last Post: 06-15-2011, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums