Link to home
Start Free TrialLog in
Avatar of BassetPatrol
BassetPatrolFlag for United States of America

asked on

VBA - Processing all files in a folder (there's a catch)

Hello,
My Goal: batch process all files in two folders
               (I can do this when working with only one root folder)
My delima: I have a root folder named 'LAS_files' and another root folder named 'Roads_dgn'.
The process preformed is basically this:
-read LAS file into Microstation (This would be obtained from the 'LAS_files' folder)
-read in the matching file from the 'Roads_dgn' folder, based on name(This would be obtained from the 'Roads_dgn' folder)
-do some stuff(which includes unloading the current .LAS file)
-Repeat the process with the next .LAS file in the 'LAS_files' folder and the next dgn file in the 'Roads_dgn' folder until all are done

I'm trying to determine the best way to accomplish this.  
I've thought about having the user enter the path to the 'Roads_dgn' folder and then using string manipulation to extract the portion of the name I would need from the 'LAS_files' file name and handle it that way.  
However I thought that perhaps there was a way using the 'batch process' for all files in one folder to apply it to two folders, one being dependent on the other.

results of the below code will show up in the Immediate window of the Visual Basic editor
Private Sub cmdJustRunAlready_Click()
Dim myFSO As New Scripting.FileSystemObject
Dim myFolder As Scripting.folder
Dim myshell As New Shell32.Shell
Dim myRootFolder As Shell32.Folder3
'Set myRootFolder = myshell.browseforfolder(0, "Pick", 0)--Original from Microstation VBA book Pg606
Set myRootFolder = myshell.BrowseForFolder(0, "Browse to .LAS folder.", 0)
Set myFolder = myFSO.GetFolder(myRootFolder.Self.Path)
ProcessFilesAndSubs myFolder
End Sub
 
Sub ProcessFilesAndSubs(FolderIn As Scripting.folder)
Dim myFile As Scripting.File
Dim mySubFolder As Scripting.folder
For Each myFile In FolderIn.Files
    Select Case UCase(Right(myFile.Name, 3))
    Case "LAS"
    Debug.Print myFile.Path
    '*************************************
   ' MsgBox ("This is where the action happens") 
    'would need to place code that needed to happen to each file here
    'At this point path name I'm working with is myFile.Path
    '************************************
    End Select
Next
For Each mySubFolder In FolderIn.SubFolders
ProcessFilesAndSubs mySubFolder
Next
 
 
End Sub

Open in new window

VBA-ReferenceFilesNeeded.bmp
VBA-FileNameInfo.bmp
Avatar of RobSampson
RobSampson
Flag of Australia image

So you've got the LAS file in
myFile.Path

You can get the dgn file by using
strDGNFile = strDGNFolderPath & "\" & Left(myFile.Name, InStr(myFile.Name, "_") - 1) & ".dgn"

then you can open that file....

Does that help?

Regards,

Rob.
Avatar of BassetPatrol

ASKER

Hi Rob,
Yes and No to the 'does that help' question.  Getting the path to the dgn file is not the real issue.  
I was wanting to take advantage of the For Each Next type code for the batch processing.  I have a few hundred files to apply this to and some of the .las files can get rather large.
I've been playing with the code and I can get it to read the first .las file (but then it prints all of the .dgn files) before printing the next .las file and then it prints all of the dgn files again.........Not the pattern I was going for.  

My adjusted code is below.
Please let me know if you think the process I am pursuing is workable.
Thanks
Rachel
Private Sub cmdJustRunAlready_Click()
 
'statements for las folder
Dim myFSO As New Scripting.FileSystemObject
Dim myFolder As Scripting.Folder
Dim myshell As New Shell32.Shell
Dim myRootFolder As Shell32.Folder3
'Set myRootFolder = myshell.browseforfolder(0, "Pick", 0)--Original from Microstation VBA book Pg606
Set myRootFolder = myshell.BrowseForFolder(0, "Browse to .LAS folder.", 0)
Set myFolder = myFSO.GetFolder(myRootFolder.Self.Path)
 
'Statements for dgn folder
Dim myFSO_dgn As New Scripting.FileSystemObject
Dim myFolder_dgn As Scripting.Folder
Dim myshell_dgn As New Shell32.Shell
Dim myRootFolder_dgn As Shell32.Folder3
Set myRootFolder_dgn = myshell.BrowseForFolder(0, "Browse to .dgn folder.", 0)
Set myFolder_dgn = myFSO_dgn.GetFolder(myRootFolder_dgn.Self.Path)
 
 
ProcessFilesAndSubs myFolder, myFolder_dgn
 
 
End Sub
 
Sub ProcessFilesAndSubs(FolderIn As Scripting.Folder, FolderIn_dgn As Scripting.Folder)
Dim myFile As Scripting.File 'Used with FolderIn
Dim myFile_dgn As Scripting.File 'Used with FolderIn_dgn
Dim mySubFolder As Scripting.Folder 'Used with FolderIn
Dim mysubFolder_dgn As Scripting.Folder 'Used with FolderIn_dgn
 
For Each myFile In FolderIn.Files 'Used with FolderIn
    Select Case UCase(Right(myFile.Name, 3)) 'Used with FolderIn
    Case "LAS" 'Used with FolderIn
    Debug.Print myFile.Path 'Used with FolderIn
    End Select 'Used with FolderIn
        
        For Each myFile_dgn In FolderIn_dgn.Files 'Used with FolderIn_dgn
        Select Case UCase(Right(myFile_dgn.Name, 3)) 'Used with FolderIn_dgn
        Case "DGN" 'Used with FolderIn_dgn
        Debug.Print myFile_dgn.Path 'Used with FolderIn_dgn
        End Select 'Used with FolderIn_dgn
    '*************************************
    'MsgBox ("This is where the action happens")
    'would need to place code that needed to happen to each file here
    'At this point path name I'm working with is myFile.Path
    '************************************
   
        Next 'Used with FolderIn_dgn--trying below
Next 'Used with FolderIn
For Each mySubFolder In FolderIn.SubFolders 'Used with FolderIn
ProcessFilesAndSubs mySubFolder, mysubFolder_dgn 'Used with FolderIn
For Each mysubFolder_dgn In FolderIn_dgn.SubFolders 'Used with FolderIn_dgn
ProcessFilesAndSubs mySubFolder, mysubFolder_dgn 'Used with FolderIn
 
Next
Next 'Used with FolderIn
 
 
End Sub

Open in new window

VBAResults.bmp
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your help.  
Your revision will allow me to accomplish my main goal.  
Great. Thanks for the grade.

Regards,

Rob.