Author Topic: Find File location?  (Read 8365 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Find File location?
« on: March 21, 2006, 11:08:45 AM »
Here is the senerio.
I need to locate the directory where the "*.tad" files are located.
To allow a location other than the ACAD search path I stipulated that the
tad files be located in the same directory as the lisp file.
FYI the tad file are text files with data to be imported by the lisp.
So if the user runs the lisp and the lisp file is not in the acad search path
how do you know what that particular path is?

I was using this and the problem is the lisp file name will change from time to
time and the "*.tad" files do not have a fixed name or may not be in the ACAD search path.

Code: [Select]
    (if (or (setq dirname (findfile (setq fn "TextInsertDCL.lsp")))
             (setq dirname (findfile (setq fn "TextInsertDCL7-6.lsp"))))
      (progn
        ;;  remove file name
        (setq dirname (vl-filename-directory dirname))
        ;; get file list from that directory
        (if (setq filelst (vl-directory-files dirname "*.tid"))
          ;;  remove the file extensions from each in list
          (setq fnamelst (mapcar 'vl-filename-base filelst))
        )
      )
       (if (> ti_debug 0)
        (prompt (strcat "\n" fn " not found"))
      )
   )


It would be better if I could tell where the lisp file was and what the name of the
lisp file is. Problem with (findfile is that it will not allow wild cards. If it did
I would just force the user to use the ACAD search path.

Any ideas?
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Find File location?
« Reply #1 on: March 21, 2006, 11:36:58 AM »
Since there's no guarantee they'll be in only one location I'd probably do something like this, and then deal with what if found (i.e. use the list with the greatest length perhaps).

REAL QUICK AND DIRTY, NOT MEANT TO REPRESENT BEAUTIFUL CODE.

Code: [Select]
(defun FindFiles ( spec / result )

    ;;  Iterate each path in AutoCAD's support path
    ;;  searching for files that meet the spec. Return
    ;;  each successful list of files as a list, the
    ;;  first member being the path.
    ;;
    ;;  e.g. (FindFiles "*.dll")
    ;;
    ;;  (   
    ;;      (   "C:\\Cad\\Acad2004\\express"
    ;;          "acetutil.dll"
    ;;          "axctextapp.dll"
    ;;          "axRText.dll"
    ;;          ...
    ;;          "propulate.dll"
    ;;          "RText.dll"
    ;;          "sysvdlg.dll"
    ;;      )
    ;;
    ;;      (   "C:\\CAD\\Acad2004\\drv"
    ;;          "CalComp8Res.dll"
    ;;          "dgwintbn.dll"
    ;;          "Dwfplot8Res.dll"
    ;;          ...
    ;;          "ps8Res.dll"
    ;;          "raster8Res.dll"
    ;;          "xes8res.dll"
    ;;      )
    ;;  )
   
    (vl-remove-if 'null
        (mapcar
           '(lambda ( path / result )
                (if (setq result (vl-directory-files path spec 1))
                    (cons path result)
                )
            )
            (   (lambda ( str delim / path result )
                    (foreach code (reverse (vl-string->list str))
                        (if (eq code delim)
                            (if path
                                (setq
                                    result (cons path result)
                                    path   nil
                                )
                            )
                            (setq path (cons code path))
                        )
                    )
                    (mapcar 'vl-list->string
                        (if path
                            (cons path result)
                            result
                        )
                    )   
                   
                )
                (getvar "acadprefix")
                (ascii ";")
            )   
        )
    )   
)

So you might use it like --

Code: [Select]
(cond
    (   (setq TadFiles (FindFiles "*.tad"))
        ;;  do something with the files,
        ;;  like load into a dcl dialog yada
    )   
)

Edit: Added example above.
« Last Edit: March 21, 2006, 11:52:47 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find File location?
« Reply #2 on: March 21, 2006, 12:01:20 PM »
Oh Boy - a new play toy. :)

Thanks Michael

I'll take it for a test drive.
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Find File location?
« Reply #3 on: March 21, 2006, 12:35:33 PM »
My pleasure Alan, hope it helps in some way.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Didge

  • Bull Frog
  • Posts: 211
Re: Find File location?
« Reply #4 on: March 21, 2006, 01:19:59 PM »
Failing all else, there is always the "shell dir *.tad /s > dirlist.txt" approach.

Its been that long since I used it, I've forgotten the correct syntax but I'm sure it'll bring back old skool memories. ;-)
Think Slow......

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find File location?
« Reply #5 on: March 21, 2006, 02:36:50 PM »
Thanks you Didge for your repply.
Michael it worked perfectly. :)
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Find File location?
« Reply #6 on: March 21, 2006, 08:02:27 PM »
Glad to hear it works Alan.

If you want something down the road that lets you scan arbitrary folder structures for files (and you don't want to rely on third party libraries like doslib) the following may interest you --

From this thread --

Code: [Select]
(defun GetFolders ( path / _GetFolders _Main )

    (defun _GetFolders ( path / folders )
        (defun _GetFolders ( path )
            (cddr
                (vl-directory-files path nil -1)
            )
        )
        (if (vl-position "."
                (setq folders
                    (vl-directory-files path nil -1)
                )
            )
            (cddr folders)
            folders
        )
    )

    (defun _Main ( path )
        (mapcar
           '(lambda ( folder / temp )
                (cons
                    (setq temp (strcat path "\\" folder))
                    (apply 'append (_Main temp))
                )
            )
            (_GetFolders path)
        )
    )

    (apply 'append (_Main path))

)

Abused version from this post --

Code: [Select]
(defun FindFilesInTree ( path spec / result )
    (vl-remove-if 'null
        (mapcar
           '(lambda ( path / result )
                (if (setq result (vl-directory-files path spec 1))
                    (cons path result)
                )
            )
            (GetFolders path)
        )
    )
)

Example use --

Code: [Select]
(findfilesintree "c:\\program files" "*.dwt")
Possible output --

Code: [Select]
(
    (
        "c:\\program files\\AutoCAD 2006\\Help\\Tutorials\\createTransmittal"
        "Washing Unit.dwt"
    )
   
    ...
   
   
    (   "c:\\program files\\AutoCAD 2006\\UserDataCache\\Template"
        "acad -Named Plot Styles.dwt"
        "acad.dwt"
        ...
        "Tutorial-iMfg.dwt"
        "Tutorial-mArch.dwt"
        "Tutorial-mMfg.dwt"
    )
   
    (   "c:\\program files\\AutoCAD 2006\\UserDataCache\\Template\\SheetSets"
        "Architectural Imperial.dwt"
        "Architectural Metric.dwt"
        ...
        "Civil Metric.dwt"
        "Manufacturing Imperial.dwt"
        "Manufacturing Metric.dwt"
    )
   
    (   "c:\\program files\\Autodesk\\DWG TrueView\\UserDataCache\\Template"
        "DWGV.dwt"
        "DWGViso.dwt"
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

chlh_jd

  • Guest
Re: Find File location?
« Reply #7 on: March 02, 2011, 12:54:44 PM »
hi MP, Alan
what about this way to getfolders , is there any faster way if no use function 'vl-directory-files'  ?
Code: [Select]
(defun dos-dir (path / foo lstn lst)
  (defun foo (path / dir lst alst)
    (if (and (setq dir (vl-directory-files path nil -1))
    (not (equal dir '("." "..")))
)
      (progn
(setq alst '("."   ".."
    "RECYCLER"   "System Volume Information"
    "RECYCLERNNKH"
   )
)
(foreach a dir
 (if (and a (not (member a alst)))
   (setq lst (cons (strcat path "/" a) lst))
 )
)
      )
    )
    lst
  )
  (setq lst  (vl-remove nil (foo path))
lstn (cons lst lstn)
  )
  (while (setq lst (vl-remove nil (mapcar 'foo lst)))
    (setq lst  (apply 'append lst)
 lstn (cons lst lstn)
    )
  )
  (acad_strlsort (apply 'append lstn))
)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Find File location?
« Reply #8 on: March 02, 2011, 02:04:58 PM »
For some improvement, once you have the full path push it to a registry value.  This is under the assumption that files don't change locations frequently, so the routine would recover the strings from previously found files first before resorting to a search.  Recovering the strings would incorporate a function to validate the logged folders to screen out any system changes, as well as an option for forcing the search (and logging that result in addition to the others).
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Find File location?
« Reply #9 on: March 04, 2011, 10:44:17 AM »
Hey Allen, do you use the Project Navigator?

I finally decided with my keynotes routine to use project specific *.key files (which sound like your *tad files)  I use the following to locate the active project directory:

Code: [Select]
(defun kb:ReturnProjectDir  (/ entry ret)
  (if (not (setq entry (vl-registry-read
                         (strcat "HKEY_CURRENT_USER\\"
                                 (vlax-product-key)
                                 "\\Recent Project List\\")
                         "Project1")))
    (setq ret (getvar "dwgprefix"))
    (setq ret (vl-filename-directory entry)))
  ret)
James Buzbee
Windows 8

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Find File location?
« Reply #10 on: March 04, 2011, 11:24:00 AM »
You guys know this is a zombie thread right?