Search code examples
lispautolisp

AutoLisp Get Last Modified


I'm trying to get the last modified date from a list of files for a sort option. I have found a source, here, where someone can update when a file was last modified, but I'm having trouble reversing the process. I keep receiving Error: Missing parameter for function. message when I try to use vlax-get and vlax-get-property.

(vl-load-com)

;; Original code
(defun SetFileLastModified ( file date / i fso fObj sh 
    oFolder oFiles iCount fItem filepath
    vTemp) 
      
    (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    (if (= (vlax-invoke fso 'FileExists file) :vlax-true)
        (progn ;; True
            (setq sh (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application"))
            (setq oFolder (vlax-invoke sh 'NameSpace (vl-filename-directory file)))
            (setq oFiles (vlax-invoke oFolder 'Items))
            (setq iCount (vlax-get oFiles 'Count))
            (setq i 0)
            (while (< i iCount)
                (setq fItem (vlax-invoke oFiles 'Item i))
                (setq filepath (vlax-get fItem 'path))
                (if (= (vl-filename-base filepath) (vl-filename-base file))(progn
                    (vlax-put fItem 'ModifyDate date);<-- Command to update the last modified date.
                ));if
                (setq i (1+ i))
            );while
            (vlax-release-object sh)
        );progn ; True
        (print (strcat "File not found: " file)); False
    );if
    (vlax-release-object fso)
);PullLastModified


;; Failed attempt
(defun PullLastModified ( file / i fso fObj sh 
    oFolder oFiles iCount fItem filepath
    vTemp) 
     
    (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    (if (= (vlax-invoke fso 'FileExists file) :vlax-true)
        (progn ;; True
            (setq sh (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application"))
            (setq oFolder (vlax-invoke sh 'NameSpace (vl-filename-directory file)))
            (setq oFiles (vlax-invoke oFolder 'Items))
            (setq iCount (vlax-get oFiles 'Count))
            (setq i 0)
            (while (< i iCount)
                (setq fItem (vlax-invoke oFiles 'Item i))
                (setq filepath (vlax-get fItem 'path))
                (if (= (vl-filename-base filepath) (vl-filename-base file))(progn
                    (princ "\nTest-01\n")
                    (setq vTemp (vlax-get fItem 'ModifyDate));<-- Having trouble pulling the modified date.
                    (princ "\nModified Date : ")(prin1 vTemp)(terpri)
                ));if
                (setq i (1+ i))
            );while
            (vlax-release-object sh)
        );progn ; True
        (print (strcat "File not found: " file)); False
    )
    (vlax-release-object fso)
);PullLastModified

Update

Found the reason why I cannot use 'DateLastModified to pull the latest modification date. Alternatives?

Data Property Values


Solution

  • That code appears to be performing many unnecessary operations when obtaining the file object: rather than using the Namespace property of the Shell object to obtain the folder and then iterating over the contents of the folder until arriving at a file with the target filename, you can obtain the file object directly using the getfile method of the File System Object (FSO).

    Consider the following code:

    (defun LM:filelastmodified ( fnm / fob fso rtn )
        (cond
            (   (not (setq fnm (findfile fnm))))
            (   (setq fso (vlax-get-or-create-object "scripting.filesystemobject"))
                (setq fob (vlax-invoke-method fso 'getfile fnm)
                      rtn (+ 2415019 (vlax-get fob 'datelastmodified))
                )
                (vlax-release-object fob)
                (vlax-release-object fso)
            )
        )
        rtn
    )
    

    The above function will return a Julian datetime representing the last modified date of the file - you can then use the edtime DIESEL function within a menucmd expression to output this as a readable datetime, e.g.:

    (defun c:test ( / fnm mod )
        (cond
            (   (not (setq fnm (getfiled "Select File" "" "" 16)))
                (princ "\n*Cancel*")
            )
            (   (not (setq mod (LM:filelastmodified fnm)))
                (princ "\nUnable to obtain last modified date.")
            )
            (   (princ (strcat "\nLast modified: " (menucmd (strcat "m=$(edtime," (rtos mod 2 15) ",yyyy-mo-dd hh:mm:ss)")))))
        )
        (princ)
    )