Avatar of gdunn59
gdunn59
 asked on

Why is the first 4 Characters of the Filename Getting Truncated

I have the following code, but when it gets to Line 17 of the code, it's truncating the beginning of the FileName, which in turn makes the Do While FindNextFile fail and then the cursor moves to "FindClose nFind".

Function FindFolders(strStartDir As String, strResults() As String) As Long 'NOTE: this is non recursive, so the results will only show for the given folder
  On Error GoTo ErrHandler
  Dim wfd As WIN32_FIND_DATA
  Dim nFind As LongPtr
  Dim strDirectoryName As String
  ReDim strResults(0) '0 will not be used
  
  'if there is already a wildcard then leave the StartDir be (example for user passed is C:\blah\T* = all folders that start with a T
  If InStr(1, strStartDir, "*") < 1 Then
    If Right(strStartDir, 1) <> "\" Then strStartDir = strStartDir & "\"
    strStartDir = strStartDir & "*"
  End If
  

  nFind = FindFirstFile(strStartDir, wfd)   'api call
  If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then     'if this is a directory
     strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
     If strDirectoryName <> "." And strDirectoryName <> ".." Then
       ReDim Preserve strResults(UBound(strResults) + 1)
       strResults(UBound(strResults)) = strDirectoryName
     End If
  End If
  Do While FindNextFile(nFind, wfd)
    If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then  'if this is a directory
      strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
      If strDirectoryName <> "." And strDirectoryName <> ".." Then
        ReDim Preserve strResults(UBound(strResults) + 1)
        strResults(UBound(strResults)) = strDirectoryName
      End If
    End If
  Loop
  
ErrHandler:
  FindClose nFind
  FindFolders = ErrorHandler(err, "FindFolders")
End Function

Open in new window


Thanks,
gdunn59
Microsoft AccessVBA

Avatar of undefined
Last Comment
gdunn59

8/22/2022 - Mon
Martin Liss

Put a breakpoint on line 17 by clicking in the left-hand margin of that line. Run the code and when it stops there, hover over wfd.cFileName. Is there what looks like a blank in position 4?
Martin Liss

Or you could try changing line 17 to

strDirectoryName = Replace(wfd.cFileName, Chr(0) , "")
ASKER CERTIFIED SOLUTION
gdunn59

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gdunn59

ASKER
Sorry, closed to quickly.  It did resolve the truncation issue with the Filename, but the Do While FindNextFile still isn't working.

Would like to reopen.

Thanks,
gdunn
Your help has saved me hundreds of hours of internet surfing.
fblack61
gdunn59

ASKER
My solution fixed the issue.  Please award Points to Martin Liss for his time.

Thanks,
gdunn59