Author Topic: Load all files (lsp fas vlx) found in directory  (Read 11451 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Load all files (lsp fas vlx) found in directory
« on: February 15, 2007, 08:07:12 AM »
I currently use 3 different functions to load all the files in a certain directory, I thought it would be fun to write a function to load any .lsp, .fas or .vls files found in a directory. Here is a sample of what I use for .lsp.

Code: [Select]
(defun load-lsp ()
  (foreach file (vl-directory-files "D:/acad_apps/" "*.lsp" 1)
           (load file (strcat file " did not load"))
           )
  )

This thread gave me the idea
TheSwamp.org  (serving the CAD community since 2003)

Chuck Gabriel

  • Guest
Re: Load all files (lsp fas vlx) found in directory
« Reply #1 on: February 15, 2007, 08:19:56 AM »
Didn't MP post something like that a year or so ago?

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Load all files (lsp fas vlx) found in directory
« Reply #2 on: February 15, 2007, 08:29:38 AM »
Didn't MP post something like that a year or so ago?

It wouldn't surprise me. :-)

Apologies, I didn't do a search before I posted.
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Load all files (lsp fas vlx) found in directory
« Reply #3 on: February 15, 2007, 08:53:01 AM »
If MP did I could not find it.
I did find this vl-directory-files + MP but not the same thing.
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.

VVA

  • Newt
  • Posts: 166
Re: Load all files (lsp fas vlx) found in directory
« Reply #4 on: February 15, 2007, 10:06:11 AM »
I use this code
Code: [Select]
;|=======================================================================================
*    Function z-files-in-directory returns the list of files being in the set directory
*    The author: Zuenko Vitaly (ZZZ)
*  http://www.autocad.ru/cgi-bin/f1/board.cgi?t=19612AN&page=2
*  Arguments:
*    directory  Path to a folder for example "D:\\My documents\\ZEF\\Lisp"
*    pattern    pattern  for example "*.lsp" or list '("*.dwg" "*.dxf")
*    nested    To search in the sub folders: t (yes) or nil (no)
* Usage:
(z-files-in-directory "D:\\My documents\\ZEF\\Lisp" "*.lsp" t)
(z-files-in-directory "D:\\My documents\\ZEF\\Lisp" '("*.lsp" "*.fas") t)
=======================================================================================|;
(defun z-files-in-directory (directory pattern nested /)
  (if (not (listp pattern))
    (setq pattern (list pattern))
    ) ;_ if
  (if nested
    (apply
      'append
      (append
  (mapcar  '(lambda (_pattern)
       (mapcar '(lambda (f) (strcat directory "\\" f))
         (vl-directory-files directory _pattern 1)
         ) ;_ list
       ) ;_ lambda
    pattern
    ) ;_ mapcar
  (mapcar
    '(lambda (d)
       (z-files-in-directory
         (strcat directory "\\" d)
         pattern
         nested
         ) ;_ z-files-in-directory
       ) ;_ lambda
    (vl-remove
      "."
      (vl-remove ".." (vl-directory-files directory nil -1))
      ) ;_ vl-remove
    ) ;_ mapcar
  ) ;_ append
      ) ;_ append
    (apply
      'append
      (mapcar '(lambda (_pattern)
     (mapcar '(lambda (f) (strcat directory "\\" f))
       (vl-directory-files directory _pattern 1)
       ) ;_ list
     ) ;_ lambda
        pattern
        ) ;_ mapcar
      ) ;_ apply
    ) ;_ if
  ) ;_ defun
Example of use
Code: [Select]
;http://discussion.autodesk.com
(defun BrowseFolder ( / ShlObj Folder FldObj OutVal)
  (vl-load-com)
  (setq
    ShlObj (vla-getInterfaceObject
       (vlax-get-acad-object)
       "Shell.Application"
     )
    Folder (vlax-invoke-method ShlObj 'BrowseForFolder 0 "" 0)
  )
  (vlax-release-object ShlObj)
  (if Folder
    (progn
      (setq
  FldObj (vlax-get-property Folder 'Self)
  OutVal (vlax-get-property FldObj 'Path)
      )
      (vlax-release-object Folder)
      (vlax-release-object FldObj)
      OutVal
    )
  )
)

; Check and set of a variable *z_root_dir* - a path to a folder of automatic loading
(if (or  (not (getenv "*z_root_dir*"))
  (= (getenv "*z_root_dir*") "")
  ) ;_ or
  (setenv "*z_root_dir*" (BrowseFolder))
)

;Loading of library
(mapcar  'load
  (z-files-in-directory (getenv "*z_root_dir*") '("*.lsp" "*.fas" "*.vlx") t)
  ) ;_ mapcar

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: Load all files (lsp fas vlx) found in directory
« Reply #5 on: February 15, 2007, 11:44:05 AM »
Dont have much time, but here is a very quick run thru...

Code: [Select]
( (lambda ( path )
    ;; a quick demonstration of how to load all the lisp files in a dir.
   (mapcar
     ;; itterate thru a list of files in directory
     (function
       (lambda ( x )
         (cond
           ;; determine if the file is of type...
           ((or
              (wcmatch x "*.lsp")
              (wcmatch x "*.vlx")
              (wcmatch x "*.fas"))
            ;; if it is, load it
            (load (strcat path x))
            )
           ) ; cond
         ) ; lambda
       ) ; function
     ( (lambda ( )
         ;; build a list of files in the given path
         (if (null path)
           ;; just in case you didnt specify a path
           nil
           (vl-directory-files path "*.*" 1)))
      )
     ) ; mapcar
   ) ; lambda
 "c:\\"
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Load all files (lsp fas vlx) found in directory
« Reply #6 on: February 15, 2007, 01:14:32 PM »
If MP did I could not find it.
I did find this vl-directory-files + MP but not the same thing.

I think this is it.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Load all files (lsp fas vlx) found in directory
« Reply #7 on: February 15, 2007, 06:42:24 PM »
Here is my spinoff:

Code: [Select]
(defun rjp-loadlisp (/ lst)
  (setq lst (vl-directory-files
      "C:\\Program Files\\AutoCAD Tools\\Lisp"
      "*.lsp"
      1
    )
lst (mapcar 'vl-filename-base lst)
  )
  (foreach x lst
    (vl-catch-all-apply 'autoload (list x (list x)))
  )
)
(rjp-loadlisp)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Adesu

  • Guest
Re: Load all files (lsp fas vlx) found in directory
« Reply #8 on: February 15, 2007, 07:05:25 PM »
Hi Mark,
I have a system how to load alls program, to my a session cad,here that code
Code: [Select]
; lap is stand for Loading Alls Program
;        Design by  : Adesu <Ade Suharna>
;        Email      : mteybid@yuasabattery.co.id
;        Homepage   : http://www.yuasa-battery.co.id
;        Create     : 20 July 2006
;        Program no.: 0383/07/2006
;        Edit by    : Adesu    04/09/2006   1).
;                              13/09/2006   2).
;                              13/10/2006   3).
;                              07/12/2006   4).
;                              19/12/2006   5).
(defun c:lap (/ all_file_name file_folder finish len load_name
      start start_hour start_minute start_second)
  (setq start (rtos (getvar "CDATE") 2 18))                       ; 5).
  (setq start_hour (atof (substr start 10 2)))                    ; 5).
  (setq start_minute (atof (substr start 12 2)))                  ; 5).
  (setq start_second (atof (substr start 14 4)))                  ; 5).
  (vl-load-com)
  (setq file_folder "D:/YBI/Program/AutoLisp/Lisp program/My Alls Program")
  (setq all_file_name (cddr (vl-directory-files file_folder)))
  (setq len (length all_file_name))                                            ; 4).
  (setq all_file_name
(mapcar '(lambda (x)(nth x all_file_name))
(vl-sort-i all_file_name '<)))
  (foreach x all_file_name
    (setq load_name (strcat file_folder "/" x))
    (load load_name)
    )                                                                          ; foreach
  (princ "\n*******loading alls program have been successful*******")          ; 1).
  (princ "\n")                                                                 ; 4).
  (princ (strcat "\n*** Total program Autolisp is = " (itoa len) " Pcs ***"))  ; 4).
  (princ "\n")                                                                 ; 5).
  (setq finish (rtos (getvar "CDATE") 2 18))                                   ; 5).
  (setq finish_hour (atof (substr finish 10 2)))                               ; 5).
  (setq finish_minute (atof (substr finish 12 2)))                             ; 5).
  (setq finish_second (atof (substr finish 14 5)))                             ; 5).
  (setq duration_hour (- finish_hour start_hour))                              ; 5).
  (setq duration_minute (abs (- finish_minute start_minute)))                  ; 5).
  (setq duration_second (abs (/ (- finish_second start_second) 100)))          ; 5).
  (gc)
  (princ
    (strcat
      "\nDURATION TIME RECORDED AS"
      "\nHour   = " (rtos duration_hour 2 0)(chr 176)
      "\nMinute = " (rtos duration_minute 2 0)(chr 180)
      "\nSecond = " (rtos duration_second 2 0)(chr 168)))                      ; 4).
  (alert "\n******* LOADING ALLS PROGRAM HAS BEEN SUCCESSFUL *******")         ; 2). 3).
  (princ)
  )                                                                            ; defun 

I currently use 3 different functions to load all the files in a certain directory, I thought it would be fun to write a function to load any .lsp, .fas or .vls files found in a directory. Here is a sample of what I use for .lsp.

Code: [Select]
(defun load-lsp ()
  (foreach file (vl-directory-files "D:/acad_apps/" "*.lsp" 1)
           (load file (strcat file " did not load"))
           )
  )

This thread gave me the idea

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Load all files (lsp fas vlx) found in directory
« Reply #9 on: February 16, 2007, 03:47:50 AM »
I currently use 3 different functions to load all the files in a certain directory, I thought it would be fun to write a function to load any .lsp, .fas or .vls files found in a directory. Here is a sample of what I use for .lsp.

Code: [Select]
(defun load-lsp ()
  (foreach file (vl-directory-files "D:/acad_apps/" "*.lsp" 1)
           (load file (strcat file " did not load"))
           )
  )

This thread gave me the idea

I have a little completed your function...  :-)

Code: [Select]
(defun load-lsp (path)
 (foreach f (apply
             (function append)
             (mapcar
              (function
               (lambda (x)
                (vl-directory-files path x 1)
               ) ;_  lambda
              ) ;_  function
              '("*.LSP" "*.FAS" "*.VLX")
             ) ;_  mapcar
            ) ;_  apply
  (load (strcat path "\\" f) (strcat path "\\" f " did not load"))
 ) ;_  foreach
) ;_  defun


(load-lsp "D:\Work")

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Load all files (lsp fas vlx) found in directory
« Reply #10 on: March 22, 2007, 09:24:38 PM »
Evgeniy,
the user would have to make sure that there weren't  x.LSP, and x.FAS and/or x.VLX in the same folder
ie MyProg.fas MyProg.lsp MyProg.vlx <<-- all these
.. that code would load each version , yes ?

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.