aspose file tools*
The moose likes Linux / UNIX and the fly likes Copying file using bat file only when there are changes to the file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Copying file using bat file only when there are changes to the file." Watch "Copying file using bat file only when there are changes to the file." New topic
Author

Copying file using bat file only when there are changes to the file.

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
This is in continuation to earlier post about attaching timestamp to copied file.

When timestamp is attached to the file, ebvery minute timestamp changes. Sp every minute same file is copied, new file will be creted with the new timestamp. Whis is just duplication files.

How can I make sure, that if templating.cfg file has been changed then only copy it otherwise not and leave the timestamp as is ?Change the timestamp only if there have been changes to the file.

Thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

This time around I'll just point you at a few clues, and see how you go with them.

You will need a way to see if there is a difference between two files. Since this is being run by a script, you dont really care what the output is, so you could tell it to be quiet in it's output.

There are two ways you can use that program. You can see if any output is produced, and use that, or (in my opinion a better way) you could use the standard diagnostic code from diff to determine if there was a change or not - you would probably want to use a bash built in capability of finding the status of the most recently executed foreground pipeline.

So - how about you see what you can come up from that?

You will also need to have some way of determining which is the last backup file you stored so you can compare it with the production version. I think there are at least 2 easy ways you can find this out:

Reference

You could store the name of the last backup file you created in a reference file. You can then read from that file to get your last saved filename.

Programatically

ls has an option to list files sorted by date. You could use this in conjunction with either the head or tail commands to restrict yourself to either the first or the last item returned from ls (depending on your sort order).

Likewise - what can you make with that?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Once new file has been created by copying. How to automatically select this new file and previous file to compare using "diff" ? IF there are differences between the two files, how to make sure that file which is created last, stays ? If there are no changes between the latest file and previous file, then the latest file shd not be saved ?

thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

I would do it the other way around - determine which file was the previous one copied, compare it to the current live configuration file, and only if they are different make a backup. Take a look at the section I wrote starting with "You will also need to have some way of determining which is the last backup file you stored..." for some ideas on that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copying file using bat file only when there are changes to the file.