Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

suggestion on how to check txt file for changes

7 REPLIES 7
Reply
Message 1 of 8
andrew.nao
589 Views, 7 Replies

suggestion on how to check txt file for changes

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 done, 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

comparing them.

 

any thought, ideas or suggestions is appreciated.

7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: andrew.nao

Hi,

 

You might want read about vl-file-systime. You could do two successive reads and compare them and allways save the last one somewhere, like in the registry or some other external file:

 

vl-file-systime


Returns last modification time of the specified file

(vl-file-systime filename)

 

Arguments: filename

A string containing the name of the file to be checked.

 

Return values:

A list containing the modification date and time, or nil, if the file is not found. The list returned contains the following elements:

 

  • year
  • month
  • day-of-week
  • day-of-month
  • hours
  • minutes
  • seconds

Note that Monday is day 1 of day-of-week, Tuesday is day 2, etc.

 

Examples:

_$ (vl-file-systime "c:/program files/AutoCAD 2002/sample/visuallisp/yinyang.lsp")
(1998 4 3 8 10 6 52)
Message 3 of 8
Kent1Cooper
in reply to: andrew.nao


@andrew.nao wrote:

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 done, 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

comparing them.

....


I don't believe you can do that.  If the display options in Windows Explorer are any indication, there's only one "Date Modified" category of information available, which as far as I can tell knows only the last time it was saved.  I don't think a file contains any information about previous times it may have been changed.  There's a "Date Created" choice, but I don't imagine that would do you any good, either -- even the first time a new file is ever saved will be more recent than that [unless it's just a copy of another file; see below].  And even if you could compare multiple modification dates, it wouldn't necessarily mean the file was changed -- someone could have opened it, looked around, changed nothing, and then unnecessarily saved it, instead of just getting out of it without saving.  That would change its "Date Modified," but its contents would be the same.  Likewise in a file that's currently open, you could compare its Date Modified to the current date/time, but there's little point in that, because the comparison will always come out the same, and it won't necessarily mean anything has been changed since the last time the file was saved.

 

If I copy a file and paste it somewhere, its Date Created is when I pasted it, but its Date Modified is still the last-modified date of the file it's a copy of, which means the copy's Date Modified is earlier than its Date Created.  In that situation, I think you can be certain that the copy has not been changed, because any saving of changes would of course move the Date Modified to later than the Date Created.  But I can't think of a way to be sure that a file in isolation has been changed since any given earlier time.

 

However, if you have two files, presumably [in the context of your question] versions of the same file in different folders or something, I think there are ways available to compare their contents, within limits.

Kent Cooper, AIA
Message 4 of 8
andrew.nao
in reply to: Kent1Cooper

@ Somebuddy

thanks Ill check out the sysvar variable

 

@Kent

I only have 1 file and yea your right about the saving and not necessarily edited the file.

 

there has to be some clever way to do this.

 

Message 5 of 8
andrew.nao
in reply to: andrew.nao

how about if i compare to filesize instead of the file date?

 

 

Message 6 of 8
Kent1Cooper
in reply to: andrew.nao


@andrew.nao wrote:

how about if i compare to filesize instead of the file date?


That's another check that may or may not give you a definitive answer.  If the file sizes are different, you could assume something about the content is, too.  [Even then, there may be no substantive difference -- it could just be a lot of added blank lines or something.]  But if someone changed a "white" to a "black," or a 10 to a 99, or any number of other kinds of changes, the file size wouldn't be any different.  As with SomeBuddy's suggestion, you would need to have saved a reference of some kind outside the file itself, to compare to, whether that's a copy of the file or a little piece of information about its size at the reference date/time.  But you're probably going to need something like that, no matter what approach you end up with.

Kent Cooper, AIA
Message 7 of 8
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....

  • year
  • month
  • day-of-week
  • day-of-month
  • ....

    (vl-file-systime "c:/program files/AutoCAD 2002/sample/visuallisp/yinyang.lsp")

    (1998 4 3 8 10 6 52)


    That's curious, in a way.  I wonder what the day-of-week entry is in there for -- I wouldn't think that would matter to most people compared to the month and date, but it must be information that someone wants at least sometimes.  But it complicates the process of comparing files' last-modified dates, if that's what someone wants to do with the information.

     

    Today would return

    (2010 9 5 17 ....)

    and next Tuesday would return

    (2010 9 2 21 ....)

     

    If you wanted to know which of two dates/times returned by that function is earlier or later, you could compare the first [year] numbers, and if they're different, you have your answer.  If they're the same, you move over one, and compare the next [month] numbers.  But if those are also the same, you can't get an answer by comparing the third [day-of-week] numbers, because in the case above, doing that would suggest that next Tuesday is before today.  But then beyond that, again you can just compare the numbers for the date and after, until those in some position come out different.

     

    So to make a comparison function, you would need to step through something like (nth) functions with positional arguments built to move up from 0 to 1 to 3 and beyond, bypassing position 2.

     

    [As an aside, I find that the function also returns one more number at the end than the AutoLISP Reference describes.  It's a three-digit integer, which I assume must be thousandths of a second.]

    Kent Cooper, AIA
    Message 8 of 8
    Anonymous
    in reply to: andrew.nao

    I wasn't talking about a system variable, but about a VLISP function and I don't think that there is a more clever way to do this other than using a function built for this purpose.

     

    Regards

    Can't find what you're looking for? Ask the community or share your knowledge.

    Post to forums  

    Autodesk Design & Make Report

    ”Boost