tags:

views:

285

answers:

5

I have small utility that does some processing on a file and changes the file extension to .processed when it is finished. I also want to delete these old .processed files after "x" number of days. Is there a file attribute that tells you when a filename was last changed? I realize that I can add white space to the end of the file after processing, re-save it, and then get the "LastWriteTime" attribute, but I do not want touch the original file at all. Any ideas?

A: 

I don't understand... LastWritetime should be the last time the file was written to. So if your processing modifies the x.processed file, you can look at that. You could also use GetLastAccessTime for the last time it was read.

Edit: Ah, renames don't modify LastWriteTime. Well then set LastWriteTime yourself

Tom Ritter
+2  A: 

LastWriteTime in the System.IO.FileInfo namespace.

FileInfo fi1 = new FileInfo(path);

Oh yeah, you can write this value as well. Update it when you update the filename.

theG
Renames specifically don't modify LastWriteTime
Ryan Brunner
Thanks, forgot to add the part about having to update the value.
theG
Freaking Sweet! Didn't realize LastWriteTime was a getter and a setter.
Crackerjack
A: 

I don't think renaming the file updates the LastWriteTime.

So when your process is renaming the file, update the LastWriteTime in the FileInfo object at the same time then you can use the LastWriteTime for your comparison.

Chris Persichetti
A: 

Even if windows did update LastWriteTime when you renamed a file, it wouldn't be good enough:

When you first change the name of a file you could check the LastWriteTime date on the new file name, but once you edit the file in any way after that the information is lost.

Fortunately, in this case you will have changed the file in other ways, so it doesn't matter. You can just check it and know that you're okay.

Joel Coehoorn
+1  A: 

Another idea if updating the LastWriteTime solution doesn't meet your needs:

Store the filename (or renamed filename) and the date the file was processed in a database.

Chris Persichetti