Browsing to select a folder and recursive file processing

Hello all,
This is a two parts question:
1. I am trying to display a Browse button for the user to select a folder (Without using the DIR and Drive list box objects), something similar to the using the Common Dialog control, but the common dialog will only allow the selection of files only. Is there a way to tweak this control to select  a folder? or is there another way in VB to do this?

2. Is there a simple way to process all files in the selected folder, including subfolders? I need to find each file and OCR it.

Thanks for your help
gfahd1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
For #1,you can use API instead of the Common Dialog Control.Try pasting this onto a form with a command button:

Private Type BrowseInfo
   hWndOwner As Long
   pIDLRoot As Long
   pszDisplayName As Long
   lpszTitle As Long
   ulFlags As Long
   lpfnCallback As Long
   lParam As Long
   iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

Private Sub Command1_Click()
   'KPD-Team 1998
   'URL: http://www.allapi.net/
   'KPDTeam@Allapi.net
   Dim iNull As Integer, lpIDList As Long, lResult As Long
   Dim sPath As String, udtBI As BrowseInfo

   With udtBI
       'Set the owner window
       .hWndOwner = Me.hWnd
       'lstrcat appends the two strings and returns the memory address
       .lpszTitle = lstrcat("C:\", "")
       'Return only if the user selected a directory
       .ulFlags = BIF_RETURNONLYFSDIRS
   End With

   'Show the 'Browse for folder' dialog
   lpIDList = SHBrowseForFolder(udtBI)
   If lpIDList Then
       sPath = String$(MAX_PATH, 0)
       'Get the path from the IDList
       SHGetPathFromIDList lpIDList, sPath
       'free the block of memory
       CoTaskMemFree lpIDList
       iNull = InStr(sPath, vbNullChar)
       If iNull Then
           sPath = Left$(sPath, iNull - 1)
       End If
   End If

   MsgBox sPath
End Sub
0
vinnyd79Commented:
For #2,try adding a listbox and a command button.Then give it a  directory to check:

Private Sub Command1_Click()
DirSearch "C:\Program Files"  ' directory to check
End Sub


Private Sub DirSearch(ByVal StartDir As String)
Dim s As String
Dim currdir As String
Dim dirlist As New Collection

List1.Clear

If Right$(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
dirlist.Add StartDir
While dirlist.Count
    'remove current directory from directory list
    currdir = dirlist.Item(1)
    dirlist.Remove 1
    'find all files and subdirectories in current, add to list
    s = Dir$(currdir, vbDirectory)
    While Len(s)
        If (s <> ".") And (s <> "..") Then    'get rid of "." and ".."
            If GetAttr(currdir & s) = vbDirectory Then  'add the subdirectory
                dirlist.Add currdir & s & "\"
            Else 'work on the file
                List1.AddItem currdir & s
            End If
        End If
        s = Dir$
    Wend
Wend
End Sub
0
gfahd1Author Commented:
Hello vinnyd79
The first part works well, Thanks alot.
As for the second part, it is only displaying the list of to level subfolders and files in the list box and not accessing the subdirectory to retrieve the file info. Looking at the code, the  GetAttr(currdir & s) is returning 2064 value and not 16 for all subfolders, therefore no recursive looping is done.
0
gfahd1Author Commented:
GetAttr(currdir & s) returns 2080 value for files and 2064 for folders.
0
vinnyd79Commented:
A better way(and faster) would be to use API for the search. Here is a great example of a recursive search using API:

http://www.mvps.org/vbnet/index.html?code/fileapi/recursivefiles_minimal.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.