Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11907
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11907
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic