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

David Bethel

  • Swamp Rat
  • Posts: 656
Re: looking for suggestions on how to check txt file for changes
« Reply #15 on: September 18, 2010, 10:41:19 AM »
You may want to look at McNeel's DOSLIB  ( dos_file )

http://wiki.mcneel.com/developer/doslib

VL knocked off quite a few of their functions, but I prefer their stuff.  My $0.02  -David
R12 Dos - A2K

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #16 on: September 20, 2010, 09:23:19 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

Stupid Fat-Fingers! lol
"How we think determines what we do, and what we do determines what we get."

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #17 on: September 20, 2010, 09:25:27 AM »
If its ok with the creators to modify their code a bit,
I think I can get something suitable for my needs

@ renderman your code isnt displaying the time


I appreciate everyones help

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #18 on: September 20, 2010, 09:41:11 AM »
@ renderman your code isnt displaying the time

The reason it didn't work for you, is because this routine was only used for testing purposes (on my end), thus I used values that simplified my testing... Poor assumption on my part, I 'spose, assuming you'd know which lines of code to customize.

More specifically, please fill in your own values for the red text within the code:

Code: [Select]


(defun c:TEST  (/ filePath dateList hour)
  (vl-load-com)
  (terpri)
  (if (setq filePath
             (getfiled "Select a Text File"
                       "[color=red]Put_YourSearchFilePath_Here[/color]"
                       "[color=red]Put_TheFIleExtensionYouWant_Here[/color]" [color=green];; <- No preceding period[/color]                       
                       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)))
           ([color=blue]strcat[/color]
             (if (= 12 hour)
               (itoa hour)
               (itoa (- hour 12)))
             ":"
             (itoa (nth 5 dateList))
             " PM"))
          ((strcat (itoa hour) ":" (itoa (nth 5 dateList)) " AM"))))))
  (terpri)
  (princ))

Edit:  For additional information, please read the Developer Documentation regarding the getfiled function.
« Last Edit: September 20, 2010, 10:33:40 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #19 on: September 20, 2010, 10:22:02 AM »
pardon my ignorance for not being more detailed in the error, Renderman
while your help is greatly appreciated

this is what is displaying
>>  "PARTS"  >>  Was Last Modified: 9-17-2010, at  PM

its not displaying the PM time.
Code: [Select]
         (cond
          (
           (<= 12 (setq hour (nth 4 dateList)))
           (progn
             (if (= 12 hour)
               (itoa hour)
               (strcat (itoa (- hour 12))
                ":"
               (itoa (nth 5 dateList))
                " PM")
             )
           )
          )
          ((strcat (itoa hour) ":" (itoa (nth 5 dateList)) " AM"

if i run just the code snippet above, it displays correctly
if i edit the original code with the above snippet, it doesnt work

the cond statements always confuse me, so im still pluggin at it but this is definitly where the issue is

thanks again

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #20 on: September 20, 2010, 10:35:48 AM »
pardon my ignorance for not being more detailed in the error, Renderman
while your help is greatly appreciated

this is what is displaying
>>  "PARTS"  >>  Was Last Modified: 9-17-2010, at  PM

its not displaying the PM time.

Please forgive *my* fat fingers... I incorrectly added a progn, in lieu of a strcat... note the blue correction in my above posted code.

Hope this helps!
"How we think determines what we do, and what we do determines what we get."

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #21 on: September 20, 2010, 11:11:43 AM »
its always good to have an extra pair of eyes
 :-o

thank you for your help

BlackBox

  • King Gator
  • Posts: 3770
Re: looking for suggestions on how to check txt file for changes
« Reply #22 on: September 20, 2010, 12:53:12 PM »
its always good to have an extra pair of eyes
 :-o

thank you for your help

You're welcome. Glad I could offer a lil' help.  :wink:
"How we think determines what we do, and what we do determines what we get."

andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #23 on: September 23, 2010, 10:08:30 AM »
ok new situation,
using the following code (thanks again for the help)
i have the code reading a line in a text file in order to compare the time the file has been modified last and then it writes the new time back to the text file for comparison the next time its modified.
I have 5 stations that need to run this lisp to let them know of any changes
if one station runs the lisp it writes back to the text file the last time the file was changed and therefore the next station who runs this lisp wont know of the changes.
which brings me to the next question.
does anyone have an idea on how i can get this to run on 5 stations without writing back untill ever station that is turned on, has ran it?
i thought of using the getvar loginname function and setting it for after i run it, it writes back, but lets say if im on vacation or out of the day, how could it write back when its just say 3 stations operating?

any thoughts are appreciated

here is the code
Code: [Select]
(defun c:stkchkfile  (/ filePath dateList hour cmdecho check_file_time check_file) ;filePath dateList hour

;;******* check for user data file last recorded modification time
(defun readfile (filename / f path line lst) ;f path line lst
(setq user_data_file (findfile "acad_user_data.txt"))
  (if (setq path user_data_file)
    (cond
      ((setq f (open path "r"))
       (while (setq line (read-line f))
(setq lst (cons line lst))
       )
       (close f)
       (reverse lst)
      )
    ) ; end cond
  ) ; end if
) ; end defun



;;*********** check for stock file modification date and time
  (terpri)
  (if (setq filePath "F:/stock/partdes.txt")
    (setq check_file
      (strcat
        "Stock book was last modified on: "
        (itoa (cadr (setq dateList (vl-file-systime filePath))))
        "-"
        (itoa (nth 3 dateList))
        "-"
        (itoa (car dateList))
        ", at "
        (cond
          ((<= 12 (setq hour (nth 4 dateList)))
           (strcat
             (if (= 12 hour)
               (itoa hour)
               (itoa (- hour 12))
             )
                ":"
               (itoa (nth 5 dateList))
               " PM")
           )
          ((strcat (itoa hour) ":" (itoa (nth 5 dateList)) " AM"))
        ) ; end cond
        "\n Please check your part numbers"
      ) ; end strcat
    ) ; end setq
  )  ; end if

(setq check_file_time
         (cond
          ((<= 12 (setq hour (nth 4 dateList)))
           (strcat
             (if (= 12 hour)
               (itoa hour)
               (itoa (- hour 12))
             )
               ":"
               (itoa (nth 5 dateList))
               " PM"
           )
          )
          ((strcat (itoa hour) ":" (itoa (nth 5 dateList)) " AM")))
)

  (terpri)
  (princ)



;;********* send alert if file has been modified

;; Ensure OpenDCL Runtime is (quietly) loaded
(setq cmdecho (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command "_OPENDCL")
(setvar "CMDECHO" cmdecho)


; call the method to load the odcl file.
(dcl_Project_Load "stkfilecheck" T)

(if (setq lst (readfile user_data_file))
(setq last_mod (nth 0 lst))
)

(if (/= check_file_time last_mod)
 (dcl_MessageBox check_file "Stockbook file checker" 2 1)
)


;;******** write back new modification time

(SETQ last_mod (SUBST check_file_time (NTH 0 lst) lst))
(SETQ m_file user_data_file)
(setq o_file (open m_file "w"))
(write-line (nth 0 last_mod) o_file)
(close o_file)

  (terpri)
  (princ)

) ;end



andrew_nao

  • Guest
Re: looking for suggestions on how to check txt file for changes
« Reply #24 on: September 23, 2010, 03:51:08 PM »
I think I solved my issue

i just copied the file that is written to on the local machine so each station has its own file to compare to instead of 1 file on the network

but i would still like to hear other peoples ideas if you have any