Wednesday, September 20, 2006

File Modified time

I had a problem recently where a program provided to me exported 60,000 files. Only some of these files were exported with the modified time was set to the year 1617.

I wrote myself a little utility to set the last write time to now for any file that was not set correctly. this is a simple utility writen in C# VS 2002. Why 2002 insteady of 2005? Honestly, 2005 was taking too long to program in. There are some bugs with it that anytime it starts to show intellisense my PC freezes for the next 20 seconds to 4 minutes. Yes, I would type and wait 4 minutes for the next character to appear. Anyway, I went back to using what I knew would work better.

My original issues were that I was trying to verify that File.GetLastWriteTime actually showed me the 1617 date, but I had mistakenly put GetLastAccessTime. This caused me a lot of pain as I paced and cursed. But when I found my own small simple bug, I fixed it.

Here is the entire code showing the count of files that are updated. I did play with the formatting a little to reduce a few lines of code.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace SetDateOfFiles
public class Form1 : System.Windows.Forms.Form
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtFolder;
private System.Windows.Forms.TextBox txtResults;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;

public Form1(){

protected override void Dispose( bool disposing ){
if( disposing ){
if (components != null) {
base.Dispose( disposing );

#region Windows Form Designer generated code
private void InitializeComponent(){
this.label1 = new System.Windows.Forms.Label();
this.txtFolder = new System.Windows.Forms.TextBox();
this.txtResults = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();

this.label1.Location = new System.Drawing.Point(24, 8);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Folder:";

this.txtFolder.Location = new System.Drawing.Point(144, 8);
this.txtFolder.Name = "txtFolder";
this.txtFolder.Size = new System.Drawing.Size(200, 20);
this.txtFolder.TabIndex = 1;
this.txtFolder.Text = "C:\\MyFiles";

this.txtResults.Location = new System.Drawing.Point(24, 88);
this.txtResults.Multiline = true;
this.txtResults.Name = "txtResults";
this.txtResults.Size = new System.Drawing.Size(480, 160);
this.txtResults.TabIndex = 2;
this.txtResults.Text = "textBox2";

this.button1.Location = new System.Drawing.Point(152, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "run";
this.button1.Click += new System.EventHandler(this.button1_Click);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 269);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Name = "Form1";
this.Text = "Form1";


static void Main() {
Application.Run(new Form1());

private void button1_Click(object sender, System.EventArgs e){
txtResults.Text = "";
string[] files = Directory.GetFiles(txtFolder.Text);
int changeCounter = 0;
int filesChecked = 0;
int totalFiles = files.Length;
foreach( String f in files ){
if( File.GetLastWriteTime(f) < new DateTime(2000, 1, 1) ){
File.SetLastWriteTime(f, System.DateTime.Now );

if( filesChecked % 10 == 0 ){
txtResults.Text = String.Format("{0} of {1} files checked\r\n{2} files changed.",
filesChecked, totalFiles, changeCounter);
txtResults.Text = String.Format("{0} of {1} files checked\r\n{2} files changed.",
filesChecked, totalFiles, changeCounter);

No comments: