Acitropy
Acitropy

Reputation: 113

Check if a a file was ran earlier the same day

I need to create a program that sets an int to 01 at the beginning of each day. Every time the file is run, the int increments up until the next day. This int will be inserted into a file name, e.g. FileName(insertdatehere)01.txt, FileName(insertdatehere02.txt, FileName(insertdatehere)03.txt, etc...

I was wondering if this is possible:

-Checking if the file already exists, and if it does, then the int value will increment. This will work since the file name has the date on it, so that each day, a new file name will be created anyways.

Am I going in the right direction, or should I completely rethink this question?

Sorry if this isn't clear, if you need me to clarify, I will.

Upvotes: 0

Views: 83

Answers (3)

Ajay Bhojak
Ajay Bhojak

Reputation: 1159

Did you try using java.util.Date class for setting time stamp , date etc. You can set date whenever the file is opened in some other file or you can set the same value at some specific place in the same file. Then whenever you open the file again you can compare and check the earlier date which is already set. This would surely help you. Firstly try it yourself and then if you remain unable to do the same post issues whichever you face.

Upvotes: 1

ktj
ktj

Reputation: 21

Your Idea is good .

But for the case when there does not exists any file and two processes are trying to create it simultaniously with same name then the problem with occur .

Above problem you can solve by using Synchronization in Java, so that code block(which contains logic for check file if it exists and creates new file) cannot be accessed simultaneously .

Upvotes: 0

HXCaine
HXCaine

Reputation: 4258

Your ideas seem correct and doing it in this way would probably work well.

Something to watch out for would be if two of the same process exist and both try to create a file assuming it doesn't exist.

As long as you consider this case, and your process runs reliably throughout the day (and you don't fall into timezone traps), you should be good to go.

Upvotes: 1

Related Questions