Thursday, July 2, 2009

Folder Comparison and Backup Management

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
//string sourceFolder = @"E:\Projects Work\Uploading Automation\RootDirectory\TestingUnzipped";
//string destinationFolder = @"E:\Projects Work\Uploading Automation\RootDirectory\Original";
//string backupPath = @"E:\Projects Work\Uploading Automation\RootDirectory\Backup";

public string sourceFolder = @"F:\UploadedFiles";
public string destinationFolder = @"F:\OriginalFiles";
public string backupPath = @"F:\BackupFiles";
public string relativePath = "";
public string OriginalPath = "";
int globalCounter = 0;


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

//foreach (string f in Directory.GetFiles(TextBox3.Text))
//{
// if (File.Exists(TextBox4.Text + f.Substring(f.LastIndexOf(@"\") + 1)))
// {
// string filename = f.Substring(f.LastIndexOf(@"\") + 1);
// string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
// //ListBox3.Items.Add(fn_withoutextn);
// File.Copy("F:\\OriginalFiles\\"+filename ,"F:\\BackupFiles\\"+filename);

// }
//}
BackupManagement();

}

public void BackupManagement()
{

DirectoryInfo dir1 = new DirectoryInfo(sourceFolder);
DirectoryInfo dir2 = new DirectoryInfo(destinationFolder);
DirectoryInfo dir3 = new DirectoryInfo(backupPath);


CheckDuplicateFiles(dir1.GetDirectories(), dir2.GetDirectories(), dir3);

}


protected void CheckDuplicateFiles(DirectoryInfo[] sourceDir, DirectoryInfo[] destDir, DirectoryInfo objDirBackup)
{

globalCounter = globalCounter + 1;
int OriginalStringLenght = 0;
int relativePathLenght = 0;




for (int countSource = 0; countSource < sourceDir.Length; countSource++)
{
for (int countDest = 0; countDest < destDir.Length; countDest++)
{
if (sourceDir[countSource].Name == destDir[countDest].Name)
{
if (globalCounter == 1)
{
OriginalPath = CreateBackUpFolderspath(sourceDir[countSource].Name);
}
else
{
relativePath = @"/" + CreateBackUpFolderspath(sourceDir[countSource].Name);
OriginalPath = OriginalPath + relativePath;
}



objDirBackup.CreateSubdirectory(OriginalPath);

foreach (string f in Directory.GetFiles(sourceDir[countSource].FullName))
{
if (File.Exists(destDir[countDest].FullName + @"\" + f.Substring(f.LastIndexOf(@"\") + 1)))
{
string filename = f.Substring(f.LastIndexOf(@"\") + 1);
string fn_withoutextn = filename.Substring(0, filename.IndexOf(@"."));
//ListBox3.Items.Add(fn_withoutextn);
backupPath = @"F:\BackupFiles";
backupPath = destDir[countDest].FullName.Replace(destinationFolder, backupPath);

Label1.Text = destDir[countDest].FullName;

File.Copy(destDir[countDest].FullName + @"\" + filename, backupPath + @"\" + filename);

}
}



if (sourceDir[countSource].GetDirectories().Length != 0)
{
CheckDuplicateFiles(sourceDir[countSource].GetDirectories(), destDir[countDest].GetDirectories(), objDirBackup);

OriginalStringLenght = OriginalPath.Length;
relativePathLenght = relativePath.Length;

OriginalPath = OriginalPath.Substring(0, OriginalStringLenght - relativePathLenght);
if (OriginalPath.Contains("/"))
relativePath = OriginalPath.Substring(OriginalPath.LastIndexOf('/') + 1);
else
relativePath = OriginalPath;
globalCounter--;
}


}
}
OriginalStringLenght = OriginalPath.Length;
relativePathLenght = relativePath.Length;
if (OriginalStringLenght > 0)
OriginalPath = OriginalPath.Substring(0, OriginalStringLenght - relativePathLenght);

if (OriginalPath.Contains("/"))
relativePath = OriginalPath.Substring(OriginalPath.LastIndexOf('/') + 1);
else
relativePath = OriginalPath;

}
}

protected string CreateBackUpFolderspath(string Name)
{
string path = Name;
int i = path.LastIndexOf("\\");
path = path.Substring(i + 1);
return path;

}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected string GetPathofRequiredFolder(string FolderName, DirectoryInfo destination)
{

}
}

Wednesday, July 1, 2009

Last inserted Row

Another method for finding Last inserted row is

ALTER PROCEDURE [dbo].[SR_SelectProjectCodeLastInserted]
AS
BEGIN
SET NOCOUNT ON
SELECT TOP 1 *
FROM Project
ORDER BY Project_Code DESC
SET NOCOUNT OFF
END

@@Identity

if you want to use the last row inserted ID then you simply do the following tasks

Declare @Request_Code INT

SELECT @Request_Code=Request_Code FROM XURS_UploadRequests WHERE Request_Code = @@IDENTITY

PRINT @Request_Code

See Article
http://www.beansoftware.com/T-SQL-FAQ/Get-Last-Inserted-ID.aspx

Friday, June 26, 2009

Working with Windows Service Using VS 2005

A Windows Serviceis a program which runs continuously in the background of an Operating System. It was formerly known as NT services. Window service internally uses the Service Control Manager (SCM) of the operating system. A service must be installed in a SCM database before it can be launched. SCM controls the entire lifetime of each installed service.

http://www.codeproject.com/KB/cs/MyWindowService.aspx

Monday, June 15, 2009

DataSet Checked Unchecked

This code is used to differenciate between two dataset which are check and which are not
DataSet dsChecked;

DataSet dsUnChecked.Tables[0].Columns.Add("Flag");


for (int counterUC = 0; counterUC < dsUnChecked.Tables[0].Rows.Count; counterUC++)
{

for (int counterCD = 0; counterCD < dsChecked.Tables[0].Rows.Count; counterCD++)
{
if (dsUnChecked.Tables[0].Rows[counterUC]["Route_Code"].ToString() == dsChecked.Tables[0].Rows[counterCD]["Route_Code"].ToString() && (dsUnChecked.Tables[0].Rows[counterUC]["Region_Code"].ToString() == dsChecked.Tables[0].Rows[counterCD]["Region_Code"].ToString()))
{
dsUnChecked.Tables[0].Rows[counterUC]["Flag"] = "1";
break;
}
else
{
dsUnChecked.Tables[0].Rows[counterUC]["Flag"] = "0";
}
}

}

ListBox Operations

Duplicate Data Check
This Code Snippet Is used to Check if Left ListBox has duplicate Data of Right List Box or not
protected void CheckDuplicateItemsFromBox(ListBox lsbRight, ListBox lsbLeft)
{
for (int countRight = 0; countRight < lsbRight.Items.Count; countRight++)
{
for (int countLeft = 0; countLeft < lsbLeft.Items.Count; countLeft++)
{
if (lsbRight.Items[countRight].Text == lsbLeft.Items[countLeft].Text)
{
lsbLeft.Items.RemoveAt(countLeft);
}
}

}
}




Move Left ListBox to Right

protected void MoveLeftListBoxToRightListBox(ListBox leftListBox, ListBox rightListBox)
{
while (leftListBox.Items.Count > 0 && leftListBox.SelectedItem != null)
{
ListItem selectedItem = leftListBox.SelectedItem;
selectedItem.Selected = false;
rightListBox.Items.Add(selectedItem);
leftListBox.Items.Remove(selectedItem);
}

}


Move Right ListBox to Left


protected void MoveRightListBoxToLeftListBox(ListBox leftListBox, ListBox rightListBox)
{
while (rightListBox.Items.Count > 0 && rightListBox.SelectedItem != null)
{
ListItem selectedItem = rightListBox.SelectedItem;
selectedItem.Selected = false;
leftListBox.Items.Add(selectedItem);
rightListBox.Items.Remove(selectedItem);
}
}

Tuesday, May 26, 2009

TextBox Enable and Disable

 function isNumberKey(evt)

{

var charCode = (evt.which) ? evt.which : event.keyCode

if (charCode > 31 && (charCode <> 57))

return false;

else

return true;

}