Thursday, March 10, 2011

C# Program to just update the timestamps of a list of file

Hi All,

Here is the program to set the create time of a set of files, using C#.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
namespace TouchFile
{
    class mainPgm
    {
        static Hashtable oHTSettings;
        static Hashtable oHTFileList;
        static void Main(string[] args)
        {
            //Check the input parameter.
            Init();
            if (args.Length ==1)
            {
                ParseXML(args[0]);

                //Do the actual "touch" here.

                TouchMe();
            }

        }

        private static void TouchMe()
        {
            try
            {
                string strDirectory="";
                string strTmpFile="";
                int i;
               
                strDirectory = oHTSettings["direcotry"].ToString();

                if (!strDirectory.EndsWith("\\"))
                {
                    strDirectory += "\\";
                }
               
                for (i = 0; i < oHTFileList.Count; i++)
                {

                    strTmpFile = strDirectory + oHTFileList[(i + 1).ToString()].ToString();
                    if (System.IO.File.Exists(strTmpFile))
                    {
                        System.IO.File.SetCreationTime(strTmpFile, DateTime.Now);
                        System.IO.File.SetLastWriteTime(strTmpFile, DateTime.Now);
                    }
                    else
                    {
                        //do something
                    }
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }
        }

        private static void Init()
        {
            //Do initialization stuff here.
            oHTFileList = new Hashtable();
            oHTSettings = new Hashtable();
        }

        private static void ParseXML(string strFilePath)
        {
            try
            {
                XmlDocument oXMLDoc = new XmlDocument();
                XmlNodeList oXMLNodes;
                int i=1;
                //XmlNode oSingleNode;

                if (System.IO.File.Exists(strFilePath))
                {
                    oXMLDoc.Load(strFilePath);
                    oXMLNodes = oXMLDoc.SelectNodes("/settings/configuration")[0].ChildNodes;

                    if (oXMLNodes != null)
                    {
                        foreach (XmlNode oSingleNode in oXMLNodes)
                        {
                            oHTSettings.Add(oSingleNode.Name, oSingleNode.InnerText);
                        }
                    }

                    oXMLNodes = oXMLDoc.SelectNodes("/settings/fileList")[0].ChildNodes;

                    if (oXMLNodes != null)
                    {
                        foreach (XmlNode oSingleNode in oXMLNodes)
                        {
                            oHTFileList.Add(i++.ToString(), oSingleNode.InnerText);
                        }
                    }

                }

            }
            catch (Exception expException)
            {
                System.Console.Write(expException.ToString());
            }
        }
    }
}





Sample XML Config file:


<xml version="1.0" encoding="utf-8"?>
<settings>
<configuration>
  <direcotry>c:\Temp</direcotry>
</configuration>
<fileList>
  <file>File1</file>
  <file>File2</file>
</fileList>
</settings>

No comments: