473,508 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare Two DataTables

Hi,
I'm filling DataTable1 with the filenames from within a directory, and
filling another DataTable2 with filenames from a SQL Db table. I'd
like to compare the two, and delete the files from the directory that
do not occur in DataTable2

StringBuilder sb = new StringBuilder();
string strDirectory = "C:\\Temp\\";

// DataTable1 ************************************************
DataTable dt = new DataTable("DiFiles");
dt.Columns.Add(new DataColumn("filename",
Type.GetType("System.String")));

System.IO.DirectoryInfo di = new DirectoryInfo(strDirectory);

// loop thru the files in the directory
foreach (FileInfo fi in di.GetFiles())
{
DataRow dr;
dr = dt.NewRow();
dr["filename"] = fi.Name;
dt.Rows.Add(dr);

sb.Append(fi.Name);
sb.Append("<br>");
}
Response.Write(sb.ToString());
//DataTable2 ***********************************************
string strCon, strSQL;
DataSet ds = new DataSet();

strCon = "***** ************* ************** ******";

strSQL = "SELECT filename FROM Files";
SqlDataAdapter da = new SqlDataAdapter(strSQL, strCon);
da.Fill(ds, "DbFiles");

// need help here on

TIA,
~Gordon
Nov 18 '05 #1
3 7617
Simple Simon wrote:
I'm filling DataTable1 with the filenames from within a directory, and
filling another DataTable2 with filenames from a SQL Db table. I'd
like to compare the two, and delete the files from the directory that
do not occur in DataTable2


Gordon,
There is no need to read files into DataTables.
Just use the File.Exists property.
e.g.

Imports System.IO
....

Dim path1 As String = "c:\temp1\"
Dim path2 As String = "c:\temp2\"

' Get the list of files from path1
Dim dirInfo As New DirectoryInfo(path1)
Dim fileInfoArray As FileInfo() = dirInfo.GetFiles()

' For each file in path1
Dim file1 As FileInfo
For Each file1 In fileInfoArray

' If the file does not existing in path2, delete it from path1
If File.Exists(path2 & file1.ToString()) = False Then
File.Delete(path1 & file1.ToString())
End If
Next

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
Nov 18 '05 #2
Thanks for the reply Carl...
But I'm comparing the files in a directory with the filenames stored
in a SQL Db table...what now?

~Gordon

On Sun, 14 Dec 2003 21:11:50 -0800, "Carl Prothman [MVP]"
<ca****@spamcop.net> wrote:
Simple Simon wrote:
I'm filling DataTable1 with the filenames from within a directory, and
filling another DataTable2 with filenames from a SQL Db table. I'd
like to compare the two, and delete the files from the directory that
do not occur in DataTable2


Gordon,
There is no need to read files into DataTables.
Just use the File.Exists property.
e.g.

Imports System.IO
...

Dim path1 As String = "c:\temp1\"
Dim path2 As String = "c:\temp2\"

' Get the list of files from path1
Dim dirInfo As New DirectoryInfo(path1)
Dim fileInfoArray As FileInfo() = dirInfo.GetFiles()

' For each file in path1
Dim file1 As FileInfo
For Each file1 In fileInfoArray

' If the file does not existing in path2, delete it from path1
If File.Exists(path2 & file1.ToString()) = False Then
File.Delete(path1 & file1.ToString())
End If
Next


Nov 18 '05 #3
Heres what i got so far...

private void Page_Load(object sender, System.EventArgs
e)
{
StringBuilder sb = new StringBuilder();
int _count = 0;

// database files
DataTable dtDatabase = DataBaseFiles();
foreach (DataRow dr in dtDatabase.Rows)
{
sb.Append(dr["fileName"].ToString());
sb.Append("<br>");
_count++;
}
sb.Append("<b>Total: </b>" +
_count.ToString());

// reset
_count = 0;
sb.Append("<br><br><br>");

// directory files
DataTable dtDirectory = DirectoryFiles();
foreach (DataRow dr in dtDirectory.Rows)
{
sb.Append(dr["fileName"].ToString());
sb.Append("<br>");
_count++;
}
sb.Append("<b>Total: </b>" +
_count.ToString());

Response.Write(sb.ToString());
}

private DataTable DataBaseFiles()
{
string strCon, strSQL;
StringBuilder sb = new StringBuilder();
DataSet ds = new DataSet();

strCon =
"Server=SIMPLE-CLIENT\\MYSQLSERVER;Trusted_Connection=true;Databa se=lacoronadetucsondb2";

strSQL = "SELECT MLNumber, ListingPictureTotal
FROM Residential";
SqlDataAdapter da = new SqlDataAdapter(strSQL,
strCon);
da.Fill(ds, "DbFiles");

strSQL = "SELECT MLNumber, ListingPictureTotal
FROM LandLot";
da.SelectCommand.CommandText = strSQL;
da.Fill(ds, "DbFiles");

strSQL = "SELECT MLNumber, ListingPictureTotal
FROM Commercial";
da.SelectCommand.CommandText = strSQL;
da.Fill(ds, "DbFiles");

DataTable dt = new DataTable();
dt.Columns.Add("fileName", typeof(string));

foreach (DataRow dr in
ds.Tables["DbFiles"].Rows)
{
int _picTotal = 0;
_picTotal = (dr["ListingPictureTotal"]
== System.DBNull.Value ? 0 :
Convert.ToInt32(dr["ListingPictureTotal"]));

for (int i = 0; i < _picTotal; i++)
{
string fileName;
if (i == 0)
fileName =
dr["MLNumber"].ToString() + ".jpg";
else
fileName =
dr["MLNumber"].ToString() + "_0" + i.ToString() + ".jpg";

if (i >= 10)
fileName =
dr["MLNumber"].ToString() + "_" + i.ToString() + ".jpg";

DataRow dr2 = dt.NewRow();
dr2["fileName"] = fileName;
dt.Rows.Add(dr2);
}
}
return dt;
}

private DataTable DirectoryFiles()
{
string strDirectory = "C:\\Temp\\";

DataTable dt = new DataTable();
dt.Columns.Add("fileName", typeof(string));

System.IO.DirectoryInfo di = new
DirectoryInfo(strDirectory);
foreach (FileInfo fi in di.GetFiles())
{
DataRow dr;
dr = dt.NewRow();
dr["filename"] = fi.Name;
dt.Rows.Add(dr);
}
return dt;
}

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2043
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
4
2242
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
1
1783
by: adeelanjum2001 | last post by:
I have two datatables with same structure. i want to compare the values in each row. for example: datatable1.rows(0).item(0) = datatable2.rows(0).item(0) one way to compare the rows in datatable...
6
1828
by: Al | last post by:
I'd like to know if there were any changes in the DataSet which was populated by reading XML file. 6 DataTables with data or just structure with no data inside of it. I think about this scenario:...
4
3282
by: Jim Andersen | last post by:
Is there anything built into .NET that is good (or rather easy) at comparing ? I have some data (in an array). I make a copy of this array, and the user changes some of the data, or maybe he...
11
35374
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
0
1512
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
7
6117
by: John Wright | last post by:
I have two datatables that I load. One I load from LDAP, the other gets loaded from Excel. I need to check to see which names are in the LDAP that are not in the Excel table and vica versa. ...
1
3190
by: pramod gupta | last post by:
How do I compare two large data tables A and B using linq and get the records which exist in DataTable A but does not exist in DataTable B.
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.