Author Topic: looking for suggestions on how to check txt file for changes  (Read 7750 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
looking for suggestions on how to check txt file for changes
« on: September 17, 2010, 02:07:43 PM »
I posted this on the acad forums and i thought i would repost this here
for other ideas.

Im looking for ideas on how I could check a text file if it has been modified.

im not looking to find out what specifically has been changed, just that if file changed in some way.

i thought about checking the date, when last saved to the current date but not really sure how to go about and was advised this wouldnt be a very good way to comparing them.

I then thought about comparing the filesize.

I would like to hear everyones opinions, thought, ideas or suggestions


thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: looking for suggestions on how to check txt file for changes
« Reply #1 on: September 17, 2010, 02:19:55 PM »
Your post is a little unclear

Do you want to compare 2 files ??
(vl-file-systime filename1) compared to (vl-file-systime filename2)

Do you want to test the Archive bit for the FileAttributes.
(dos_attrib [filespec [bits]])

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.

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #2 on: September 17, 2010, 02:23:29 PM »
Your post is a little unclear

Do you want to compare 2 files ??
(vl-file-systime filename1) compared to (vl-file-systime filename2)

Do you want to test the Archive bit for the FileAttributes.
(dos_attrib [filespec [bits]])



no, im not looking to compare 2 files.

im just looking for a way to find out if a specific file has changed
and send an alert to the user that this file has changed
i dont need to know what was changed, just that something in that file was modified

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: looking for suggestions on how to check txt file for changes
« Reply #3 on: September 17, 2010, 02:35:40 PM »
Have a look at
(dos_fileex filename)

but to my mind you'd need data to test against

ie
'Changed since when' ?

[ps:added]
What are the rules for testing Andrew ?
« Last Edit: September 17, 2010, 02:45:27 PM by Kerry »
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.

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #4 on: September 17, 2010, 03:00:12 PM »
Have a look at
(dos_fileex filename)

but to my mind you'd need data to test against

ie
'Changed since when' ?

[ps:added]
What are the rules for testing Andrew ?

crap i dont have that dos lib function.
im working with 2009 and dos lib version 8  :x

rules for testing... umm if the file was edited and soemthing was added or removed or if the file size grew or got smaller then a set filesize


Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: looking for suggestions on how to check txt file for changes
« Reply #5 on: September 17, 2010, 03:02:23 PM »
I'm with Kerry in that you would need something to 'check against' - else perhaps check DateLastModified to see whether it lies in a date range close to the current date?

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #6 on: September 17, 2010, 04:15:05 PM »
I'm with Kerry, and Lee... you need something to compare against (file has been modified in the last 24 hours, etc.).

[edit]
To be redundant...

rules for testing... umm if the file was edited

...Since when?

soemthing was added or removed

...As compared to what file (i.e., a copy, a backup, etc.)?

if the file size grew or got smaller then a set filesize

...In relation to what file size?
[/edit]

In any event, perhaps this may provide 'some' of what Andrew is looking for:

Code: [Select]
(defun c:TEST  (/ filePath dateList hour)
  (vl-load-com)
  (terpri)
  (if (setq filePath
             (getfiled "Select a Text File"
                       "F:\\I\\WM_Custom-09\\LSP-Custom\\Custom\\"
                       "lsp"
                       8))
    (princ
      (strcat
        "\n  >>  \""
        (strcase (vl-filename-base filepath))
        "\"  >>  Was Last Modified: "
        (itoa (car (setq dateList (vl-file-systime filePath))))
        "-"
        (itoa (cadr dateList))
        "-"
        (itoa (nth 3 dateList))
        ", at "
        (cond
          ((<= 12 (setq hour (nth 4 dateList)))
           (progn
             (if (= 12 hour)
               (itoa hour)
               (itoa (- hour 12)))
             ":"
             (itoa (nth 5 dateList))
             " PM"))
          ((strcat (itoa hour) ":" (itoa (nth 5 dateList)) " AM"))))))
  (terpri)
  (princ))

« Last Edit: September 17, 2010, 04:21:20 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: looking for suggestions on how to check txt file for changes
« Reply #7 on: September 17, 2010, 05:16:00 PM »
I usually find the FSO methods easier to use and more accurate:

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 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)
)

PS> Welcome to TheSwamp Renderman, I'm sure you'll like it here very, very much.  :-)

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #8 on: September 17, 2010, 05:36:09 PM »
I usually find the FSO methods easier to use and more accurate:

That's a biased statement, if I ever heard one! lol

Would "Wscript.shell" , or "Scripting.FileSystmeObject" also be able to extract the file's owner ID, perhaps? If so, that *may* lend to the OP's much needed comparison criteria.

PS> Welcome to TheSwamp Renderman, I'm sure you'll like it here very, very much.  :-)

Thanks, Lee!
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: looking for suggestions on how to check txt file for changes
« Reply #9 on: September 17, 2010, 06:15:24 PM »
I usually find the FSO methods easier to use and more accurate:

That's a biased statement, if I ever heard one! lol

*shrug*

Would "Wscript.shell" , or "Scripting.FileSystmeObject" also be able to extract the file's owner ID, perhaps? If so, that *may* lend to the OP's much needed comparison criteria.

Difficult.

But this seems to work:

Code: [Select]
;;--------------------=={ Get File Owner }==------------------;;
;;                                                            ;;
;;  Returns the Owner of the specified file                   ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  file - full filename of file to query                     ;;
;;------------------------------------------------------------;;
;;  Returns:  \\DomainName\\AccountName , else nil            ;;
;;------------------------------------------------------------;;

(defun LM:GetFileOwner ( file / _Release FSO result WMILocator WMIService RefDomName AccName )
  (vl-load-com)
  ;; © Lee Mac 2010

  (defun _Release ( obj )
    (if obj (vl-catch-all-apply 'vlax-release-object (list obj)))
  )

  (setq FSO (vlax-create-object "Scripting.FileSystemObject"))
  (cond (   (zerop (vlax-invoke fso 'FileExists file)))
   
        (t  (vlax-for item
              (setq result
                (vlax-invoke
                  (setq WMILocator (vlax-create-object "WbemScripting.SWbemLocator")
                        WMIService (vlax-invoke WMILocator 'ConnectServer "." "root\\cimv2"))
                  'ExecQuery
                  (strcat "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" file
                    "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
                  )
                )
              )
              (setq RefDomName (vlax-get item 'ReferencedDomainName))
              (setq AccName    (vlax-get item 'AccountName)))))

  (mapcar '_Release (list result WMIService WMILocator FSO))

  (if (and RefDomName AccName) (strcat "\\\\" RefDomName "\\" AccName))
)

« Last Edit: September 17, 2010, 06:24:37 PM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: looking for suggestions on how to check txt file for changes
« Reply #10 on: September 18, 2010, 12:04:27 AM »
I can't get (vlax-create-object "Scripting.FileSystmeObject") to work  :roll:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: looking for suggestions on how to check txt file for changes
« Reply #11 on: September 18, 2010, 02:07:43 AM »
I can't get (vlax-create-object "Scripting.FileSystmeObject") to work  :roll:
How about with "Scripting.FileSystemObject" ??


[ps:added]
or was that a rhetorical statement :-D
« Last Edit: September 18, 2010, 02:14:00 AM by Kerry »
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.

pkohut

  • Bull Frog
  • Posts: 483
Re: looking for suggestions on how to check txt file for changes
« Reply #12 on: September 18, 2010, 03:10:28 AM »
Run an md5 or some other hash on the contents of the file, store the hash value for later comparison. One way is with the utility provided in the link. Or get a wscript that can calc and validate a hash. Either way should be callable via vlisp.

http://support.microsoft.com/kb/841290

« Last Edit: September 18, 2010, 03:15:52 AM by pkohut »
New tread (not retired) - public repo at https://github.com/pkohut

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: looking for suggestions on how to check txt file for changes
« Reply #13 on: September 18, 2010, 07:34:16 AM »
Would "Wscript.shell" , or "Scripting.FileSystmeObject" also be able to extract the file's owner ID, perhaps? If so, that *may* lend to the OP's much needed comparison criteria.

I can't get (vlax-create-object "Scripting.FileSystmeObject") to work  :roll:

 :-D

ronjonp

  • Needs a day job
  • Posts: 7529
Re: looking for suggestions on how to check txt file for changes
« Reply #14 on: September 18, 2010, 10:24:41 AM »
I can't get (vlax-create-object "Scripting.FileSystmeObject") to work  :roll:
How about with "Scripting.FileSystemObject" ??


[ps:added]
or was that a rhetorical statement :-D

I was being a smart@$$  :-P  back to our regularly scheduled program.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC