TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Sebb77 on October 04, 2010, 10:56:12 AM

Title: Retrieve file creation date
Post by: Sebb77 on October 04, 2010, 10:56:12 AM
I wonder if there's a way to retrieve the date a file was created, in LiSP. And i mean any file type, not just the current drawing.
Thanks for any hint.
Title: Re: Retrieve file creation date
Post by: Lee Mac on October 04, 2010, 11:07:53 AM
Some examples:

Code: [Select]
;;-----------------=={ 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
)

;;--------------------=={ File Created }==--------------------;;
;;                                                            ;;
;;  Returns the DateCreated 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:  Creation Date (Julian), else nil                ;;
;;------------------------------------------------------------;;

(defun LM:FileCreated ( 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 'DateCreated)))))
 
  (vlax-release-object fso)

  date
)

;;--------------------=={ File Info }==-----------------------;;
;;                                                            ;;
;;  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:                                                  ;;
;;  List of file information:                                 ;;
;;   <FileName>         ~ name of file                        ;;
;;   <DateCreated>      ~ Julian date of file creation        ;;
;;   <DateLastAccessed> ~ Julian date of file last accessed   ;;
;;   <DateLastModified> ~ Julian date of file last modified   ;;
;;   <Size>             ~ Size of file in bytes               ;;
;;------------------------------------------------------------;;

(defun LM:FileInfo ( file / fso fObj info )
  (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))

            (setq info (mapcar '(lambda ( property functionn ) ((eval functionn) (vlax-get fObj property)))
                         '(Name DateCreated DateLastAccessed DateLastModified Size)
                         '((lambda ( x ) x )
                           (lambda ( x ) (+ x 2415019))
                           (lambda ( x ) (+ x 2415019))
                           (lambda ( x ) (+ x 2415019))
                           (lambda ( x ) x ))))))
 
  (vlax-release-object fso)

  info
)

(defun LM:FormatDate ( date format )
  ;; © Lee Mac 2010
  (menucmd (strcat "m=$(edtime," date "," format ")"))
)

;; Test Function

(defun c:test ( / f i )
  (vl-load-com)
 
  (if (and (setq f (getfiled "Select File to Query" "" "" 16))
           (setq i (LM:FileInfo f)))

    (princ
      (strcat
        "\n-- Information for: " (car i)
        (apply 'strcat
          (mapcar 'strcat '("\nCreated: " "\nAccessed: " "\nModified: ")
            (mapcar '(lambda ( x ) (LM:FormatDate (rtos x 2 15) "DD.MO.YYYY HH:MM:SS")) (cdr i))
          )
        )
        "\nSize (KB): " (rtos (/ (last i) 1024.) 2 3)
      )
    )
  )

  (princ)
)

Another test function calling the above subs:

Code: [Select]
(defun c:GetCreatedTest ( / f )

  (if (setq f (getfiled "Select File to Query" "" "" 16))
    (print
      (LM:FormatDate (rtos (LM:FileCreated f) 2 15) "DD.MO.YYYY HH:MM:SS")
    )
  )

  (princ)
)
Title: Re: Retrieve file creation date
Post by: MP on October 04, 2010, 11:13:05 AM
There are a myriad of ways to do it but a useful exercise imo is to figure out how to use the information here (http://technet.microsoft.com/en-us/library/ee198732.aspx) in a lisp program. Here (http://www.theswamp.org/index.php?topic=35156.0) is an example.

Edit: Feck I am way too slow today. Medic bring me a coffee stat.
Title: Re: Retrieve file creation date
Post by: Sebb77 on October 04, 2010, 01:17:02 PM
Thank you guys,
      i am looking closely to your examples. Exactly want i wanted.
Title: Re: Retrieve file creation date
Post by: Lee Mac on October 04, 2010, 01:19:18 PM
You're very welcome Sebb  :-)
Title: Re: Retrieve file creation date
Post by: Andrea on November 16, 2010, 04:10:42 PM
Lee,..

I also Have something similar...
but when time is come to compare 2 files...eaven if the file have been copied..
the result are diffrent.

Code: [Select]
(defun checkDateCreate (file / fso)
  (setq fso
(vla-getinterfaceobject
   (vlax-get-acad-object)
   "Scripting.FileSystemObject"
)
  )
  (rtos
    (vlax-get-property
      (vlax-invoke-method fso 'getfile file)
      'DateCreated
      ;'DateLastAccessed
      ;'DateLastModified
    ) 2 10
  )
)
Title: Re: Retrieve file creation date
Post by: rocknor on December 14, 2021, 05:07:09 PM
When we have to change last modification date of file:

Code: [Select]
       ;; ------------------------<[** Set Last Modified [date] of [file] **]>--------------------------------------------
       ;; Created By: © PaNo 2021 
       ;; patkai.norbi@gmail.com
       ;;
       ;; Example: (SetFileLastModified "f:\\works\\lisp\\test\\somefile.dll" "01/01/2015 8:00:00 AM")
(defun SetFileLastModified ( file date / i fso fObj sh) 
          (vl-load-com)       
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq sh (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application"))
  (if (= (vlax-invoke fso 'FileExists file) -1)
    (progn
   (setq oFolder (vlax-invoke sh 'NameSpace (vl-filename-directory file)))
   (setq oFiles (vlax-invoke oFolder 'Items))
   (setq count (vlax-get oFiles 'Count))
   (setq i 0)
   (while (< i count)
     (setq fItem (vlax-invoke oFiles 'Item i))
(setq filepath (vlax-get fItem 'path))
(if (= (vl-filename-base filepath) (vl-filename-base file))
   (vlax-put fItem 'ModifyDate date)    
)
     (setq i (+ i 1))
   ) ;wend
   (vlax-release-object sh)
    )
    (print (strcat "File not found: " file))
  )
  (vlax-release-object fso)    
    )
   
Title: Re: Retrieve file creation date
Post by: JohnK on December 14, 2021, 05:42:22 PM
Welcome to theSwamp, rocknor.
Title: Re: Retrieve file creation date
Post by: rocknor on December 15, 2021, 05:00:34 PM
Thank you, hello everybody:)