Solved

Browsing to select a folder and recursive file processing

Posted on 2003-11-24
5
474 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:gfahd1
  • 3
  • 2
5 Comments
 
LVL 28

Assisted Solution

by:vinnyd79
vinnyd79 earned 100 total points
ID: 9812938
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9812975
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
 

Author Comment

by:gfahd1
ID: 9813601
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
 

Author Comment

by:gfahd1
ID: 9813687
GetAttr(currdir & s) returns 2080 value for files and 2064 for folders.
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 100 total points
ID: 9813720
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now