Re-occuring loop...?

Discussion in 'AutoCAD' started by REscamilla, Feb 9, 2005.

  1. REscamilla

    REscamilla Guest

    Has anyone written a re-occuring loop to gather a path and all sub-folders
    within sub-folders, etc.etc..

    I'm a bit tired....

    And crashing...

    (defun search-folder (path / di cn dlist p opath)

    (setq di (vl-directory-files path nil -1)
    ln (length di)
    cn 0
    )

    (while (nth cn di)
    (if (not (member (strcat path (nth cn di)) dlist))
    (progn
    (if (or
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    )
    (progn
    (setq dlist (cons (strcat path (nth cn di)) dlist))
    (if (or
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    )
    (progn
    (setq npath (strcat path "\\" (nth cn di)))
    (search-folder npath)
    )
    ))
    (setq cn (1+ cn))

    ))
    ))

    (reverse dlist)
    )
     
    REscamilla, Feb 9, 2005
    #1
  2. REscamilla

    Jeff Mishler Guest

    Give this a try.......

    (defun vl-dir-tree (p)
    (foreach f (vl-directory-files p nil -1)
    (if (not (or (eq f ".")
    (eq f "..")
    )
    )
    (progn
    (vl-dir-tree (strcat p "\\" f))
    (setq tree (cons (strcat p "\\" f) tree))
    )
    )
    )
    )

    ;;Sample usage:
    (setq tree nil)
    (setq tree (vl-sort (vl-dir-tree "C:\\Base Maps") '<))
     
    Jeff Mishler, Feb 9, 2005
    #2
  3. REscamilla

    REscamilla Guest

    A little nutty'r but getting closer...I think...

    (defun search-folder (path / di cn dlist p opath)

    (setq di (vl-directory-files path nil -1)
    cn 0
    )

    (while (nth cn di)
    (if (not (member (strcat path (nth cn di)) dlist))
    (progn
    (if (and
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    )
    (progn
    (setq dlist (cons (strcat path (nth cn di)) dlist))
    (if (and
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    )
    (progn
    (setq npath (strcat path "\\" (nth cn di)))
    (prompt (strcat "\n" (nth cn di)))
    (search-folder npath)
    )



    )
    )
    )
    )
    )
    (prompt (strcat "\n" (nth cn di)))
    (setq cn (1+ cn))
    ); end of while
    (reverse dlist)
    )
     
    REscamilla, Feb 9, 2005
    #3
  4. REscamilla

    REscamilla Guest

    I'll give a try, but in the meantime...

    I actually tried to use foreach first, but kept giving me an over flow
    error...
     
    REscamilla, Feb 9, 2005
    #4
  5. REscamilla

    REscamilla Guest

    \\\\ four quacks...?

    is that allowed?



     
    REscamilla, Feb 9, 2005
    #5
  6. REscamilla

    Jeff Mishler Guest

    ?????
     
    Jeff Mishler, Feb 9, 2005
    #6
  7. REscamilla

    REscamilla Guest

    I ran yours, but it was slower than mine, and with fewer directories...I
    exclude repeated directories...so I don't get it...

    I've got mine working, take a look and see if you find anything wrong...but
    mine seems to run faster and gathers more...

    (defun search-folder (path / di cn p opath)

    (setq di (vl-directory-files path nil -1)
    cn 0
    )

    (while (nth cn di)
    (if (not (member (strcat path (nth cn di)) dlist))
    (progn
    (if (and
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    (not (= (nth cn di) "\\"))
    )
    (progn
    (setq dlist (cons (strcat path (nth cn di)) dlist))
    (if (and
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    (not (= (nth cn di) "\\"))
    )
    (progn
    (setq npath (strcat path (nth cn di)))

    (search-folder npath)
    )



    )
    )
    )
    )
    )
    (setq cn (1+ cn))
    ); end of while
    (reverse dlist)
    )
     
    REscamilla, Feb 9, 2005
    #7
  8. REscamilla

    REscamilla Guest

    Oh, excuse the mess... and the "\\"


     
    REscamilla, Feb 9, 2005
    #8
  9. REscamilla

    Jeff Mishler Guest

    Well, I ran yours and was only returned 3 entries in the dlist and then gave
    an error......I found where it was occurring and fixed it. I also modified
    my code slightly. I then ran both on my sample folder, that has a total of
    208 folders in it, using the following code for time purposes:
    (setq sTime (getvar "millisecs"))
    (setq tree nil)
    (setq tree (vl-sort (vl-dir-tree "C:\\Base Maps") '<))
    (length tree)
    (setq eTime (getvar "millisecs"))
    (princ (strcat "\nElapsed time: " (rtos (- etime stime) 2 2) "
    milliseconds..."))


    (setq sTime (getvar "millisecs"))
    (setq dlist nil)
    (setq dlist (search-folder "C:\\Base Maps"))
    (length dlist)
    (setq eTime (getvar "millisecs"))
    (princ (strcat "\nElapsed time: " (rtos (- etime stime) 2 2) "
    milliseconds..."))

    These are the results, mine first:
    121565421
    nil
    ("C:\\Base Maps\\Aerials" "C:\\Base Maps\\Assessor" ......clipped)
    208
    121567953
    Elapsed time: 2532.00 milliseconds...

    121567968
    nil
    ("C:\\Base Maps\\Aerials" "C:\\Base Maps\\Assessor" ......clipped)
    208
    121570468
    Elapsed time: 2500.00 milliseconds...

    So yes, yours was 0.032 seconds faster than mine ........

    Here's your code as I modified it:

    (defun search-folder (path / di cn p opath)

    (setq di (vl-directory-files path nil -1)
    cn 0
    )

    (while (nth cn di)
    (if (not (member (strcat path (nth cn di)) dlist))
    (progn
    (if (and
    (not (= (nth cn di) "."))
    (not (= (nth cn di) ".."))
    )
    (progn ;;removed duplicate test here
    (setq dlist (cons (strcat path "\\" (nth cn di)) dlist));;added "\\"
    (setq npath (strcat path "\\" (nth cn di)));;added "\\"
    (search-folder npath)
    )
    )
    )
    )
    (setq cn (1+ cn))
    ) ; end of while
    (reverse dlist)
    )


    And just for grins, my modified code:

    (defun vl-dir-tree (p / dirs)
    (setq dirs (vl-remove-if '(lambda (x)
    (or (eq "." x)
    (eq ".." x))
    )
    (vl-directory-files p nil -1))
    )
    (foreach f dirs
    (vl-dir-tree (strcat p "\\" f))
    (setq tree (cons (strcat p "\\" f) tree))
    )
    )
     
    Jeff Mishler, Feb 9, 2005
    #9
  10. REscamilla

    REscamilla Guest

    For some reason, there is a lag on your approach...

    Was wondering if mapcar could not be used...

    I tried mine using (vmon) and it ran even faster, processing my entire
    network in 1 mil of a second....I tweaked it a bit more last night...

    But I still think that the utility has a few flaws...I will review...I'd
    still like it to process even faster...

    In the end it will not only return all the directories, but gather dwg
    names, then use dbx to modify and copy, and I'd like to process everything
    and keep it to that split second time...

    I've found that creating sub functions work much faster than keeping all
    processing in one function...
     
    REscamilla, Feb 9, 2005
    #10
  11. REscamilla

    Jeff Mishler Guest

    I had tried mapcar, it was actually slightly slower. I would really like to
    see your tweaked code. As I said before, your code only returned 3 folders
    before erroring out so it seemd to be fast, but the results were useless.
    Once I added the "\\" to 2 ares of your code it returned all of the folders
    that mine did (which happened to be exactly what should have been returned),
    but the speed was nearly the same as mine. Using (vl-directory-files) seems,
    to me, to be the 'slow' portion of the code so I'm curious how you are
    getting results in an instant......
     
    Jeff Mishler, Feb 9, 2005
    #11
  12. REscamilla

    Jeff Mishler Guest

    So I had a few more moments of free time and I played with the
    FileSystemObject to accomplish this task. For the using the same test folder
    and timing as yesterday, my new lisp runs in 578 millisecs vs my 2500+ ms
    ........ I'd say the FSO is a wee bit quicker ;-)

    After some more testing, I can get the entire folder structure of my C:\\
    drive written to a txt file in about 90 seconds. This txt file is 389k in
    size!
     
    Jeff Mishler, Feb 10, 2005
    #12
  13. REscamilla

    REscamilla Guest

    Sorry it took long for me to get back... still adding some more...

    So what did you do?

    I untweaked mine and now it's running slower...I'll have to go back again
    from where I started...
     
    REscamilla, Feb 10, 2005
    #13
  14. REscamilla

    Jeff Mishler Guest

    Heh, I thought I'd cut-n-pasted the code.......obviously I missed a step
    somewhere ;-)

    Here's the code for using the Windows FileSystemObject:

    (defun dir-tree (p / fso folder subfolders)
    (setq fso (vlax-create-object "Scripting.FilesystemObject"))
    (setq folder (vlax-invoke fso 'getfolder p))
    (setq subfolders (vlax-get folder 'subfolders))
    (vlax-for subfldr subfolders
    (dir-tree (vlax-get subfldr 'path))
    (setq tree (cons (vlax-get subfldr 'path) tree))
    )
    (vlax-release-object folder)
    (vlax-release-object fso)
    tree
    )

    And this is how I stored my entire drive folder structure:

    (setq f (open "c:\\testtree.txt" "w"))
    (mapcar '(lambda (x)
    (write-line x f)
    )
    (reverse (dir-tree "C:\\"))
    )
    (close f)

    --
    Jeff
    check out www.cadvault.com
     
    Jeff Mishler, Feb 10, 2005
    #14
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.