Results 1 to 6 of 6
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DIR bad for me (Word/Xp et al.)

    I'm rapidly coming to the conclusion that i could do a lot worse than remove all use of the DIR statement from my VBA code.

    For years I've written code that grabs files from folders, building arrays of folder names before recursing down through layers. It's almost always worked almost all of the time. Tonight another hiccough. I have a Word/VBA aplication working its way through Drive C. It just happens to be grabbing VBA code from Excel workbooks, and while it had turned its attention to Excel I inadvertently (a.k.a. "stupidly") clicked on a command button that just happened to make use of DIR. By the time the other code returned from Excel, the DIR doo-dad was thoroughly confused, and my strFileName=DIR and my While Not StFileName ="" got hopelessy lost.

    DIR has only one copy of itself, so my making a second initialization call to it robbed it of its history from the first set of calls.

    I think I should systematically eradicate DIR from my code and make use either of the FSO or of the SHELL as outlined in various posts in this forum.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: DIR bad for me (Word/Xp et al.)

    <!profile=Drew>Drew<!/profile> shows in <post#=319305>post 319305</post#> how to use a collection to store the results of a Dir loop, for instance to be able to search subfolders interactively.

    FileSearch (available in Office applications) provides another way to loop through files. You can create multiple FileSearch objects.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DIR bad for me (Word/Xp et al.)

    > collection to store the results of a Dir loop

    Thanks, Hans. I think the Collections is a nice twist on my string array, but poses for me the same problem - if something "out there", perhaps an invoked sub-application/macro, makes use of DIR, I'm shot. (This ought to be ridiculously easy to demonstrate). I often write applications that iterate throuigh sets of files, hence DIR, and for each file invoke a process-on-a-document. If that process makes use of DIR, problems arise.

    > FileSearch ... provides another way
    What's the correct protocol here? I'm in half a mind to thank you for this valuable lead, and yet, and yet ..... I know that in a week's time I'm going to investigate this technique, too, whereas i thought I'd be comparing just two techniques. Now it will be three.

    Perhaps "thanks a lot, Hans", will do, with a "(grin!)"?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: DIR bad for me (Word/Xp et al.)

    Perhaps the ancient Chinese curse: may you live in interesting times. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  5. #5
    Star Lounger
    Join Date
    Jan 2001
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DIR bad for me (Word/Xp et al.)

    > i thought I'd be comparing just two techniques. Now it will be three.

    Why not make it four, and make the fourth one the best one?
    Play with this , and you'll find there's no comparison.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DIR bad for me (Word/Xp et al.)

    > Why not make it four

    Thanks, Don (gloom) and thanks for the link (bright!). More stuff to read through inb my Copious Free Time(TM).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •