Results 1 to 3 of 3

Thread: [RESOLVED] Dir() reentrancy?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Resolved [RESOLVED] Dir() reentrancy?

    I have a directory with about 30,000 files with a type, say, .xxx. Almost every one of these files has a file of type .yyy, type .zzz or type .yymmdd (where yymmdd is a date) associated with it.

    I have to find each .xxx file that has an associated file and rename the .xxx file. Finding the file is easy, in a loop using Dir(). However, if I use Dir() within the loop to find the associated file, the outer loop's Dir() no longer works - it's not a reentrant function.

    I can use fso.FindFile inside the loop, but FindFile doesn't accept wildcards, and the dates run from Jan. 1, 2005 to the current day, so the inner loop would execute over 14 million times, and I'm not sure I'll live long enough to see the results. Can anyone think of a more elegant way of doing this? Is there an API I can use that accepts wildcards and that isn't being used by the Dir() function? Am I overlooking a simple solution?

    TIA

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Dir() reentrancy?

    There are a number of ways to approach this problem.
    As you have so many files to check, it may make sense to just
    enumerate the files in the directory once and then sort out what to do with
    them as a secondary process.

    Here's one example:
    VB Code:
    1. '
    2. ' ClassModule: FileInfo
    3. '
    4. Option Explicit
    5.  
    6. Public Name As String
    7. Public FullPath As String
    8. Public HasXXX As Boolean
    9. Public HasYYY As Boolean
    10. Public HasZZZ As Boolean
    11. Public HasYMD As Boolean
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Dim strDir As String
    5.   Dim files As New Collection
    6.   Dim strExt As String
    7.   Dim strPath As String
    8.   Dim file As FileInfo
    9.  
    10.   ' Set the path to be enumerated for files
    11.   strPath = "C:\SomeFolder\"
    12.   ' Get the first file in the folder
    13.   strDir = Dir(strPath & "*.*", vbNormal)
    14.   ' Loop this code until there are no more files
    15.   Do While Len(strDir) > 0
    16.     ' Get the files extension
    17.     strExt = JustExt(strDir)
    18.     ' If it's an XXX, YYY or ZZZ extension or the extension
    19.     ' is a valid date in the format YYMMDD, then process it
    20.     If InStr(";xxx;yyy;zzz;", ";" & LCase(strExt) & ";") Or _
    21.        IsDate(Format(strExt, "@@-@@-@@")) Then
    22.        ' Add the file to our collection of files
    23.        Call AddFile(files, strPath & strDir)
    24.     End If
    25.     ' Get the next file in the folder
    26.     strDir = Dir
    27.   Loop
    28.  
    29.   ' List the files that have an XXX extension and a file
    30.   ' association with a YYMMDD format extension
    31.   List1.Clear
    32.   For Each file In files
    33.     If file.HasXXX And file.HasYMD Then
    34.       Call List1.AddItem(file.FullPath & file.Name & ".xxx")
    35.     End If
    36.   Next
    37. End Sub
    38.  
    39. Private Sub AddFile(ByRef files As Collection, ByVal strFile As String)
    40.   Dim file As New FileInfo
    41.   Dim strExt As String
    42.   Dim flgExists As Boolean
    43.  
    44.   ' Trap an error caused if the file isn't
    45.   ' already in the collection
    46.   On Local Error GoTo FileNotFound
    47.   ' Attempt to get the file from the collection
    48.   Set file = files(JustStem(strFile))
    49.   ' If we get this far, the file exists
    50.   flgExists = True
    51.  
    52. FileNotFound:
    53.   ' Reset the error handler
    54.   On Local Error GoTo 0
    55.  
    56.   ' Get the File's extension
    57.   strExt = JustExt(strFile)
    58.    
    59.   ' Assign the Name of the file (without extension)
    60.   file.Name = JustStem(strFile)
    61.   ' Assign the Path of the file
    62.   file.FullPath = JustPath(strFile)
    63.  
    64.   ' If the file has an XXX extension, set the XXX flag
    65.   If LCase(strExt) = "xxx" Then
    66.     file.HasXXX = True
    67.    
    68.   ' If the file has an YYY extension, set the YYY flag
    69.   ElseIf LCase(strExt) = "yyy" Then
    70.     file.HasYYY = True
    71.    
    72.   ' If the file has an ZZZ extension, set the ZZZ flag
    73.   ElseIf LCase(strExt) = "zzz" Then
    74.     file.HasZZZ = True
    75.    
    76.   ' If the file has an YYMMDD extension, set the YMD flag
    77.   ElseIf IsDate(Format(strExt, "@@-@@-@@")) Then
    78.     file.HasYMD = True
    79.    
    80.   End If
    81.  
    82.   ' If the file is in the collection, remove it
    83.   If flgExists Then
    84.     Call files.Remove(file.Name)
    85.   End If
    86.   ' Add the file to the collection
    87.   files.Add file, file.Name
    88. End Sub
    89.  
    90. ' Returns just the Path of the supplied filename
    91. Private Function JustPath(ByVal fileName As String) As String
    92.   If InStr(fileName, "\") > 0 Then
    93.     fileName = Left(fileName, InStrRev(fileName, "\"))
    94.   Else
    95.     fileName = ""
    96.   End If
    97.   JustPath = fileName
    98. End Function
    99.  
    100. ' Returns just the File of the supplied file/path name
    101. Private Function JustFile(ByVal fileName As String) As String
    102.   If InStr(fileName, "\") > 0 Then
    103.     fileName = Mid(fileName, InStrRev(fileName, "\") + 1)
    104.   End If
    105.   JustFile = fileName
    106. End Function
    107.  
    108. ' Returns just the stem portion of the filename
    109. ' (the part without the extension)
    110. Private Function JustStem(ByVal fileName As String) As String
    111.   fileName = JustFile(fileName)
    112.   If InStr(fileName, ".") > 0 Then
    113.     fileName = Left(fileName, InStrRev(fileName, ".") - 1)
    114.   End If
    115.   JustStem = fileName
    116. End Function
    117.  
    118. ' Returns just the file's extension
    119. Private Function JustExt(ByVal fileName As String) As String
    120.   Dim strExt As String
    121.   If InStr(fileName, ".") > 0 Then
    122.     strExt = Mid(fileName, InStrRev(fileName, ".") + 1)
    123.   End If
    124.   JustExt = strExt
    125. End Function
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Dir() reentrancy?

    I never even thought of atttacking it that way, Aaron. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width