OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-04-2004, 19:46
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default Finding a File in Sub Folders

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
This works well enough if it finds the file, but if not file exsits I get stuck in a loop. Any ideas of a better way to do this? Is using the FileSystemObject a good way to go about this, I found no way in Draw to do it.

Thanks,

Shelby
Reply With Quote
  #2  
Old 12-04-2004, 07:31
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default Re: Finding a File in Sub Folders

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
Reply With Quote
  #3  
Old 12-04-2004, 17:15
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default Half way there

Alex your suggestion works great. But another problem. My code will only go one folder deep. I need it to keep digging if it finds another folder. Make sense?

Thanks,

Shelby
Reply With Quote
  #4  
Old 12-04-2004, 18:13
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default FileSystemObject

Also....is there a better way to do the same thing without calling the FileSystemObject? Thanks,

Shelby
Reply With Quote
  #5  
Old 12-04-2004, 22:22
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default Re: FileSystemObject

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
And here is how you can use it:

Code:
Public Sub Test()
    ProcessFolder "D:\Temp"
End Sub
This will call ProcessFile subroutine for every CDR file found in D:\Temp or any of its subfolders.

I hope this helps.
Reply With Quote
  #6  
Old 15-04-2004, 14:54
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default Problem

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
Reply With Quote
  #7  
Old 15-04-2004, 20:43
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default Re: Problem

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
...
That's instead of the following lines:

Code:
    For n = 1 To Folders.Count 
        EnumSubFolders Folders(n), Folders 
    Next n
Reply With Quote
  #8  
Old 16-04-2004, 09:46
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default THANKS

This is Perfect Alex. Again THANKS for the HELP.

Shelby
Reply With Quote
  #9  
Old 09-05-2004, 15:06
olympiatr
Guest
 
Posts: n/a
Default Re: THANKS

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
Reply With Quote
  #10  
Old 09-05-2004, 18:22
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,782
Blog Entries: 11
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default Code

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -5. The time now is 00:04.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com