Looking for advice on passing variables between functions.

Looking for advice on passing variables between functions.

Anonymous
Not applicable
1,279 Views
10 Replies
Message 1 of 11

Looking for advice on passing variables between functions.

Anonymous
Not applicable

I've written a short little script here. The intent was when supplied with the file directory,  file extension and a list, it would create a list of all file with that extensions. For each item in that list, it would then strcat the file name with the last modified date pulled by the wonderful Lee Mac's Function and then append the combined string to the list provided.

 

(defun DirectoryList ( dir typ olist / filelist file new fp)
	(setq filelist (vl-directory-files dir typ 1))				;get file list
	(foreach file filelist										;for each file in list
		(progn
			(setq ext (vl-filename-extension file))
			(setq fp (strcat dir "\\" file))
			(cond
				((or (= ext ".dwg")
				     (= ext ".dcl")
				 )
					(progn
						(setq v (rtos (LM:FileLastModified fp)))
						(setq new '(strcat file "|" v))
					)
				)
			)
		(setq olist (append new olist))
		)														;Adds new concat string to list.
	)
)

;;-----------------=={ File Last Modified }==-----------------;;
;;                                                            ;;
;;  Returns the Last Modified property of a file in Julian    ;;
;;  time                                                      ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  file - full filepath of file to query                     ;;
;;------------------------------------------------------------;;
;;  Returns:  Last Modified Date (Julian), else nil           ;;
;;------------------------------------------------------------;;

(defun LM:FileLastModified ( file / fso fObj date )
  (vl-load-com)
  ;; © Lee Mac 2010

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (cond (   (zerop (vlax-invoke fso 'FileExists file)))

        (   (setq fObj (vlax-invoke-method fso 'GetFile file)
                  date (+ 2415019 (vlax-get fObj 'DateLastModified)))))
  
  (vlax-release-object fso)

  date
)

As is, it steps though each item found, but doesn't seem to be append the list provided by the function argument.

 

Any ideas on how to resolve this?

 

0 Likes
Accepted solutions (3)
1,280 Views
10 Replies
Replies (10)
Message 2 of 11

ronjonp
Mentor
Mentor

On thing that stands out is this. The ' will create a literal evaluation.

(setq new '(strcat file "|" v))

(setq new '(strcat file "|" v))
;; eval to -> (STRCAT FILE "|" V)

And another thing:

image.png 

Message 3 of 11

Anonymous
Not applicable

Thank you for the clear visual, but I'm not sure I understand. I was under the impression that the syntax is structured like: 

(defun test ( arg1 arg2.... / localvar1 localvar2....)

So I was trying to use the olist as an argument so any list could be passed through this function, but are you saying some arguments need to be on the other side of the "/"?

 

 

 

0 Likes
Message 4 of 11

ronjonp
Mentor
Mentor
Accepted solution

olist is your result the way you have it structured. The directory you supply should result in a list of files that you can then process ... here's a quick mod to what you posted:

(defun directorylist (dir / ext filelist fp new olist v)
  (setq filelist (vl-directory-files dir "*" 1)) ;get file list
  (foreach file	filelist		;for each file in list
    (progn (setq ext (strcase (vl-filename-extension file)))
	   (setq fp (strcat dir "\\" file))
	   (cond ((wcmatch ext ".DWG,.DCL")
		  (setq v (rtos (lm:filelastmodified fp)))
		  (setq new (strcat file "|" v))
		 )
	   )
	   (setq olist (cons new olist))
    )					;Adds new concat string to list.
  )
  (reverse olist)
)
(directorylist "c:\\Your\\path\\here")

 

Message 5 of 11

Sea-Haven
Mentor
Mentor
Accepted solution

A shorter way to load the great lee-mac lisps is to just check if the defun name exists and if not the load the lisp, no need for all the code to live in each program.

 

; thanks to lee-mac for this routine www.lee-mac.com

(if (not lm:filelastmodified )(load "file last modified"))

 

Message 6 of 11

Anonymous
Not applicable

Ok, I think I get it now. Thank you for your help.

0 Likes
Message 7 of 11

Anonymous
Not applicable

Actually, one last question: I just noticed that you used 'cons' and 'reverse' to add items to the olist. Is there a reason that you did this method instead of using "append'?

0 Likes
Message 8 of 11

ronjonp
Mentor
Mentor

 


@Anonymous wrote:

Actually, one last question: I just noticed that you used 'cons' and 'reverse' to add items to the olist. Is there a reason that you did this method instead of using "append'?


Just personal preference. With append you need to supply a list where as cons you can supply a direct value:

(setq r (append '(1) r))
(setq r (cons 1 r))
Message 9 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... you used 'cons' and 'reverse' to add items to the olist. Is there a reason that you did this method instead of using "append'?


 

Which raises the question:  Is the (reverse) necessary?  If it just makes a list of things that will be stepped through, and each thing in the list dealt with in some way, does it matter what order they're in?  I would think you could omit the (reverse) entirely.  I agree that (cons) is simpler than (append) as described, but it only sometimes matters that the resulting list end up in the same order as were the things it was built from.

Kent Cooper, AIA
0 Likes
Message 10 of 11

ronjonp
Mentor
Mentor

@Kent1Cooper wrote:

@Anonymous wrote:

.... you used 'cons' and 'reverse' to add items to the olist. Is there a reason that you did this method instead of using "append'?


 

Which raises the question:  Is the (reverse) necessary?  If it just makes a list of things that will be stepped through, and each thing in the list dealt with in some way, does it matter what order they're in?  I would think you could omit the (reverse) entirely.  I agree that (cons) is simpler than (append) as described, but it only sometimes matters that the resulting list end up in the same order as were the things it was built from.


Reverse is not necessary in this case, just a habit of mine to return items in the order they were collected. 8-)

 

Maybe this is more appropriate if the OP is displaying this info somewhere:

(acad_strlsort olist)
0 Likes
Message 11 of 11

Anonymous
Not applicable
Accepted solution

One last thing, 

 

IF I were to need to adapt this code to only pull an initial file list with a specific extension, I should only need to make the following change? So X is an argument to provide the file extension like so 

(directorylist "c:\\Your\\Path\\Here" ".dwg")

 

(defun directorylist (dir X / filelist fp new olist v)
  (setq filelist (vl-directory-files dir X 1)) ;get file list

The rest of the logic seems like it would still work, only needing a condition for if the file extension doesn't match the .dwg provided, so that a list is still output. I went ahead an tested this, and it doesn't seem to be returning a list. Any ideas?

 

Nevermind, I solved this myself. The function call needed a wildcard in the extension.

(directorylist "c:\\Your\\Path\\Here" "*.dwg")

0 Likes