473,395 Members | 1,774 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,395 software developers and data experts.

how can i get the file list of a directory based on some logic

e.g. c:\test

doc1.txt
doc1.pdf
doc2.txt
doc2.pdf
doc3.txt
doc4.pdf
doc5.txt
doc5.pdf

only doc1, doc and doc5 will be returned as has both .txt and .pdf files

how to implement? i'm not sure whether the DirectoryInfo's GetFileInfo's
seach pattern can do so or not?


Nov 16 '05 #1
4 1757
Use Directory.GetFiles(...);

I'd do this to start with:

ArrayList GetUnionFileList ( string path, string extension1, string
extension2 )
{
ArrayList unionList = new ArrayList();
try
{
string[] filesWithExt1 = Directory.GetFiles ( path,
extension1 );
string[] filesWithExt2 = Directory.GetFiles ( path,
extension2 );

foreach ( string file1 in filesWithExt1 )
{
foreach ( string file2 in filesWithExt2 )
{
if ( Path.GetFileNameWithExtension (
file1 ).ToLower() == Path.GetFileNameWithExtension ( file2 ).ToLower() )
{
unionList.Add ( Path.GetFileNameWithExtension
( file1 ) );
break;
}
}
}
}
catch ( Exception ex )
{
// handle the exception here
}
finally
{
return unionList;
}
}
This will be a slow algorithm for lots of files, so I'd look for ways to
improve it. (EG, removing each found item from the array so they're not
searched again).

Hope that helps.

Dan.
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
e.g. c:\test

doc1.txt
doc1.pdf
doc2.txt
doc2.pdf
doc3.txt
doc4.pdf
doc5.txt
doc5.pdf

only doc1, doc and doc5 will be returned as has both .txt and .pdf files

how to implement? i'm not sure whether the DirectoryInfo's GetFileInfo's
seach pattern can do so or not?

Nov 16 '05 #2
Hi,

I've got proposition for you:

1) Create two hashtables:
Hashtable txtHTab=new Hashtable();
Hashtable pdfHTab=new Hashtable();

2) Fill these hashtables with file names without extension
e.g. for TXT
string dirPath="c:\\test";
string[] fileNames=Directory.GetFiles(dirPath, "*.txt");
for(int i=0; i<fileNames.Length; i++) {
txtHTab.Add(Path.GetFileNameWithoutExtension(fileN ames[i])
, fileNames[i]);
}

.... do the same thing with PDFs

3) Iterate through txtHTab.Keys to search this key by:
pdfHTab.Contains(fileNameWithoutExt)
-if this key is in PDFs then you're sure that exists
TXT and PDF file with the same name prefix.

HTH
Marcin
e.g. c:\test

doc1.txt
doc1.pdf
doc2.txt
doc2.pdf
doc3.txt
doc4.pdf
doc5.txt
doc5.pdf

only doc1, doc and doc5 will be returned as has both .txt and .pdf files

how to implement? i'm not sure whether the DirectoryInfo's GetFileInfo's
seach pattern can do so or not?

Nov 16 '05 #3
Hi Mullin yu,

A third solution:

ArrayList doubleFiles = new ArrayList();

string[] files = Directory.GetFiles(Application.StartupPath, "*.txt");

foreach(string s in files)
{
string t = Path.GetFileNameWithoutExtension(s);
if(File.Exists(t + ".pdf"))
doubleFiles.Add(t);
}

Substitute the extensions and path as needed, preferrably in a method like
Dan's

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
This is the winner, in my opinion anyway. Faster than 1 and easier to
understand than implementing hash tables.

;o)
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsja14nbyklbvpo@pbn_computer...
Hi Mullin yu,

A third solution:

ArrayList doubleFiles = new ArrayList();

string[] files = Directory.GetFiles(Application.StartupPath, "*.txt");

foreach(string s in files)
{
string t = Path.GetFileNameWithoutExtension(s);
if(File.Exists(t + ".pdf"))
doubleFiles.Add(t);
}

Substitute the extensions and path as needed, preferrably in a method like
Dan's

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #5

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

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
0
by: Eric | last post by:
I have this same problem, and I've *half-way* resolved it. It turns out that this is not exactly an IO problem; it's actually a security issue and maybe even an ASP.NET bug. Here is what I've...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
4
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
9
by: Claudio Grondi | last post by:
I am aware, that it is maybe the wrong group to ask this question, but as I would like to know the history of past file operations from within a Python script I see a chance, that someone in this...
10
by: Paul | last post by:
Hi I am using the HtmlInputFile control to upload a file from a client to a server. I have a browse to find the file on the server but need to create the path dynamically as to were it will go...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: want2learnperl | last post by:
Perl Newbie here. I want to open a file that I have setup as a configuration file. After opening the file I was to assign parts of the words to variables. for example (configuration file...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.