![]() |
|
#1
|
||||
|
||||
|
All our design files start with a 5 digit number like this: "12345 My File.cdr' I am trying to write a script that lets you type in the 12345 to a dialog box and it will find and open the file for you. I can do this easy enough for one folder, my problem is the file may be in a sub folder. Here is what I have come up with so far:
Code:
Dim fs As FileSystemObject
Dim txtFileName As String
Dim f As Folder
Dim f1 As Folder
Dim fc As Folders
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(frmOBCNconfig.txtPath.Value)
Set fc = f.SubFolders
txtFileName = Dir(frmOBCNconfig.txtPath.Value & "\" & txtControlNumber & "*.cdr")
If txtFileName = "" Then
Do While txtFileName = ""
For Each f1 In fc
txtFileName = Dir(f1.Path & "\" & txtControlNumber & "*.cdr")
Next
Loop
Else
End If
Thanks, Shelby |
|
#2
|
||||
|
||||
|
I don't see a reason for your WHILE-loop. Once you are done with your For Each, you have scanned all the subfolders. What you need to do, is to break out of the For Each loop as soon as you find something:
Code:
If txtFileName = "" Then
For Each f1 In fc
txtFileName = Dir(f1.Path & "\" & txtControlNumber & "*.cdr")
If txtFileName <> "" Then Exit For
Next f1
End If
|
|
#5
|
||||
|
||||
|
Ok,
I'll let you into a little trick of how to process all the subfolders. You still can use the Dir function to enumerate folders, however it is not "re-enterable" that is, you cannot dig into subfolders while you keep enumerating files/folders. You need to process all the files/folders in the current folder, then go one level deeper. What I do is to create a list of folders. I start enumerating subfolders in a given folder. As they are found, they are added to the list. Then I have a loop through every element in the list and enumerate subfolders of those folders. As I keep processing one list element after another, new folders are added to the end of the list, so later I will get back to them and process their subfolders. And so on until all the elements in the ever-growing list are processed. By the time I'm done, I have a complete list of fully qualified paths for every folder and their subfolders. Here is the implementation of the idea: Code:
Option Explicit
Private Sub EnumSubFolders(ByVal SrcFolder As String, ByVal Folders As Collection)
Dim f As String
f = Dir(SrcFolder & "\*.*", vbDirectory)
While f <> ""
If f <> "." And f <> ".." And _
(GetAttr(SrcFolder & "\" & f) And vbDirectory) <> 0 Then
Folders.Add SrcFolder & "\" & f
End If
f = Dir()
Wend
End Sub
Private Sub ProcessFolder(ByVal SrcFolder As String)
Dim f As String, sFolder As Variant
Dim Folders As New Collection
Dim n As Long
' Create a list of folders and subfolders
EnumSubFolders SrcFolder, Folders
For n = 1 To Folders.Count
EnumSubFolders Folders(n), Folders
Next n
' Process the CDR files in each of the folders found
For Each sFolder In Folders
f = Dir(sFolder & "\*.cdr")
While f <> ""
ProcessFile sFolder & "\" & f
f = Dir()
Wend
Next sFolder
End Sub
Private Sub ProcessFile(ByVal sFile As String)
' Do your file processing here
End Sub
Code:
Public Sub Test()
ProcessFolder "D:\Temp"
End Sub
I hope this helps. |
|
#6
|
||||
|
||||
|
Alex I can't seem to get this to work. It only digs two folders deep. I have created a folder structure like this: c:\test\one level\two level\three level\four level\filename.cdr
I added a msgbox after this line in the Sub EnumSubFolders: f = Dir() MsgBox f I call the sub like this: ProcessFolder "c:\test" The message box reports: .. one level blank message box .. two level blank message box Shouldn't it enumerate all the folders? Thanks for the help, Shelby |
|
#7
|
||||
|
||||
|
Hmm,
That is very strange. For some reason I assumed that For...Next loop evaluates its conditions on each iteration, but it appears that it doesn't. The thing is that the Folders collection grows as the loop progresses, but the For statement never checks for the new values and always uses the value as it was when the first iteration started. Anyway, I had to rework it by using While..Wend loop instead: Code:
...
n = 1
While n <= Folders.Count
EnumSubFolders Folders(n), Folders
n = n + 1
Wend
...
Code:
For n = 1 To Folders.Count
EnumSubFolders Folders(n), Folders
Next n
|
|
#9
|
|||
|
|||
|
hi friend i am watching your codes and i tried the codes but i get a error "FileSystemObject". why i got this message? can u tell me? (sometimes i'm gettin this message) thanx... btw my mail: olympiatr@yahoo.com
|
|
#10
|
||||
|
||||
|
If you use Alex's code he posted above, there is no need for the FileSystemObject. I now use his code, and it seems to be working well.
If you would like to post your code, we can look at it, to see what the problem might be. Shelby |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| delete vba project from cdr file | hotairballoon | CorelDRAW/Corel DESIGNER VBA | 1 | 18-05-2005 09:08 |
| File Convertor to do transpararent TIFFs | dan | CorelDRAW/Corel DESIGNER VBA | 8 | 22-12-2004 13:23 |
| using "importfromfile" command without file name d | olympiatr | CorelDRAW/Corel DESIGNER VBA | 3 | 14-08-2004 02:39 |
| New macros: Oberon Selection Manager & File Name inserte | Alex | Site News | 0 | 24-08-2003 12:28 |
| Open - Resize - Savefile in Photo Paint | greenee | Corel Photo-Paint VBA | 2 | 28-06-2003 03:54 |