473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Find the same files on disk

Here, there is a challenge: to find the files must be on a local computer,
have the same names.

Get drives and folders to go through without problems. But what and how to
store files while a search?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
gift
Dec 3 '07 #1
3 2313
"Alexander Vasilevsky" <ma**@alvas.netschrieb im Newsbeitrag
news:fj***********@behemoth.volia.net...
Here, there is a challenge: to find the files must be on a local computer,
have the same names.

Get drives and folders to go through without problems. But what and how to
store files while a search?
either List<stringor List<FileInfo>

Christof

Dec 3 '07 #2
It depends how robust it needs to be... if this is a one-off task,
then a simple dictionary over the name (key) and list of matching
paths (value) would probably suffice, but it wouldn't be very elegant
(or efficient) and would have a lot of false-positives without more
restriction than just the name. Very crude, but it works.

Marc

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
// lazy hacky static lookups
static readonly Dictionary<string, List<string>allFiles = new
Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
static int counter = 0;

static void Main()
{
// find available drives
List<stringfixedDisks = new List<string>();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.DriveType == DriveType.Fixed)
{
fixedDisks.Add(drive.Name);
}
}
Walk(fixedDisks.ToArray());

// output duplicates
foreach (KeyValuePair<string, List<string>pairs in allFiles)
{
if (pairs.Value.Count 1)
{
Console.WriteLine("***" + pairs.Key);
foreach (string path in pairs.Value)
{
Console.WriteLine(path);
}
}
}
}

static void Walk(string[] paths)
{
// expolde the gives paths
for(int i = 0; i < paths.Length; i++) {
string path = paths[i];
if ((counter++ % 100) == 0)
{ // cheap progress indicator
Console.WriteLine(path);
}

try
{// look at files
string[] files = Directory.GetFiles(path);
for (int j = 0; j < files.Length; j++)
{
string file = files[j], key =
Path.GetFileName(file);
List<stringlookups;
if (!allFiles.TryGetValue(key, out lookups))
{
lookups = new List<string>();
allFiles.Add(key, lookups);
}
lookups.Add(file);
}
}
catch(Exception ex)
{
Console.Error.WriteLine(path + ":" + ex.Message);
}

try
{ // look at sub-dirs
Walk(Directory.GetDirectories(path));
}
catch (Exception ex)
{
Console.Error.WriteLine(path + ":" + ex.Message);
}
}
}
}
Dec 3 '07 #3
Hi,

A dictionary, the key is the name of the file the value the number of times
that you found that file.

I hope you know how to traverse a folder struct.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Alexander Vasilevsky" <ma**@alvas.netwrote in message
news:fj***********@behemoth.volia.net...
Here, there is a challenge: to find the files must be on a local computer,
have the same names.

Get drives and folders to go through without problems. But what and how to
store files while a search?

http://www.alvas.net - Audio tools for C# and VB.Net developers +
Christmas gift


Dec 3 '07 #4

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

Similar topics

0
by: F. Da Costa | last post by:
Hi all, Although i'm not a complete newbie i do feel like one at the moment. This question concerns the use of the ResourceBundle as per the example in the Trail tutorial on java.sun.com (thus...
1
by: John Benson | last post by:
I checked out the http://c2.com/cgi/wiki?TreeInSql link and found the following: Motivation There are many applications where data is structured as trees. This data needs to be stored in...
5
by: Ben Jeurissen | last post by:
Hello, I have to deal with the following issue in C++: Two threads are started from the main thread, both capturing images from a different firewire camera. Both threads take shots of 460800...
6
by: davidmdalle | last post by:
Hello, I have been having a bit of trouble finding help on the safest way to move data files to a different disk on the same server. Most help is about moving data files to a different sqlserver. ...
6
by: Neo The One | last post by:
On my development machine, I often delete all folders/files under C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files. That has not caused me any trouble til now. So now we have...
1
by: Leslie Osborne | last post by:
I need to clear up a point concerning the note I posted a few days ago. I said the applications I have written in Visual Basic 3.0 Professional edition will run on a computer which contains some...
3
by: Amritha.Datta | last post by:
Can anyone tell me why the below code returns error? Exception Details: System.IO.FileNotFoundException: Could not find file Dim strLFolder As String = "c:\Temp\F Files" Dim intClientID As...
6
by: =?Utf-8?B?U2NvdHQgVHJpY2s=?= | last post by:
I followed the instructions from MSDN for Webclient UploadFile and I get an error: Could not find file 'C:\testfile.xls'. If I add the file (c:\testfile.xls) to the server I do not get the error...
1
by: =?Utf-8?B?UkIncw==?= | last post by:
My eMachine when boot up, will ask "Press F11 for recovery". I just leave it and it will boot to WinXP. this eMachine just died. I hooked up its HD as slave to other computer, try to move my own...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.