How to test a text file is newly created?

W

www

Hi,

My program first generate a text file, then read it and process it. The
problem of my current program is that, if running failed, that text file
generated last run will be read and processed.

I hope to check a check to see if the text file is newly generated. It
it is not, that means it is generated by previous run and current run
has failed. The program then stop and will not proceed to read that file.

Could you show me how to do this? Thank you very much.
 
R

rossum

Hi,

My program first generate a text file, then read it and process it. The
problem of my current program is that, if running failed, that text file
generated last run will be read and processed.

I hope to check a check to see if the text file is newly generated. It
it is not, that means it is generated by previous run and current run
has failed. The program then stop and will not proceed to read that file.

Could you show me how to do this? Thank you very much.
Create a file called myfile-new.txt.
Read it and process it.
Rename the file you have just read to myfile-old.txt

If the new file and the old file have different names then the program
will not pick up the old file when it looks for the new one.

rossum
 
G

GArlington

Hi,

My program first generate a text file, then read it and process it. The
problem of my current program is that, if running failed, that text file
generated last run will be read and processed.

I hope to check a check to see if the text file is newly generated. It
it is not, that means it is generated by previous run and current run
has failed. The program then stop and will not proceed to read that file.

Could you show me how to do this? Thank you very much.

How about simple process the file -> delete or archive it? Then after
successful processing run there will be NO file to process, the new
file will ONLY appear AFTER successful generation run.
Is that too trivial?
 
W

www

GArlington said:
How about simple process the file -> delete or archive it? Then after
successful processing run there will be NO file to process, the new
file will ONLY appear AFTER successful generation run.
Is that too trivial?

I cannot delete that file, because the file is one of the products I
need. What do you mean "archive it"? One reply suggested:

the program generates myfile_new.txt
the program read and process it
in the end, the program rename myfile_new.txt to myfile_old.txt

That is a reasonable way to achieve my purpose.

Another way suggested by another reply is:
I should set up my program in such a way: if the program fails to
generate that text file, through an exception, then the program will not
proceed to read the text file.

Thank you all for your replies. I greatly appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top