Saturday, May 31, 2008

A simple Code to rename the Jpg file



This is a simple coding for rename the filename, I want to share my code to my viewers..Here i compare the name of the employee with the filename..if both are equal then i changed the file name in to employee IC name
public static void ProcessDir(string sourceDir, int recursionLvl,string Location,string Destdir)
{

string Filename = string.Empty;
string errmsg = string.Empty;
string DirectoryPath=string.Empty;
DataTable dt = new DataTable();


dt = Tools.GetRecord("select * from employee where deleted=0 and dateofleft is null", ref errmsg);
if (dt.Rows.Count == 0)
{
Filename = "Error";
MessageBox.Show( Filename );
}
DirectoryPath =sourceDir.ToString();
DirectoryInfo dir = new DirectoryInfo(DirectoryPath);
FileInfo[] bmpfiles = dir.GetFiles("*.jpg");
foreach (FileInfo f in bmpfiles)
{
string Name = string.Empty;
Name = f.Name.ToString();
Name = Name.Replace(".jpg", "");
foreach (DataRow dr in dt.Rows)
{

if (dr["Employeename"].ToString() == Name)
{
////Filename = Filename + Name;
string Path = string.Empty;
Path = Destdir.ToString()+"\\"+ dr["EmployeeID"].ToString() + ".jpg";
try
{
System.IO.File.Move(f.FullName.ToString(), Path.ToString());
}
catch (FileNotFoundException ex)
{
MessageBox.Show(Name + Path+"File Not Found"+ex.ToString());
continue;

}
//MessageBox.Show("Successfully Renamed");
}
}
}



}


Screen shots

1 comment:

Hellan said...

Technical reviews are usually long-winded, but Bala has done a tremendous job of cutting down on the non-essentials. Short & sweet (as well as visual) is in place. Keep doing it!