-1

Possible Duplicate:
C# file exists by file name pattern

let's say I have a folder called documents, the documents folder consisting of pdfs, videos, texts, photos etc... I would like to do something with only .doc extensions. The only thing that I can think is actually creating a process that will use cmd.exe and use dir command in the directory, however I am looking for something more efficient. Any recommendations?

Community
  • 1
  • 1
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103

2 Answers2

2

Perhaps you're referring to the DirectoryInfo.GetFiles method:

foreach (FileInfo fi in new DirectoryInfo(@"C:\documents").GetFiles().Where(x => x.Extension.ToLower() == ".doc")) {
    Console.WriteLine(fi.Name);
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

Directory.EnumerateFiles Method

var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.doc*", SearchOption.AllDirectories);

foreach (string currentFile in txtFiles)
{
    ...
}

If you want something more efficient:

The good folks over at http://visuallogparser.codeplex.com/ have provided us with the source code.

Open the VisualLogParser solution in VS2010, ignore the prompt about debugging, after the solution loads, F5, set the combo-box to FS (FileSystem), paste in this query and press go.

SELECT
    ContentPath, [Days (Old)], FileName, [Creation Date Time]
    USING creationtime AS [Creation Date Time],
    TO_DATE([Creation Date Time]) AS Cdate,
    SUB(TO_LOCALTIME(SYSTEM_TIMESTAMP()), Cdate) AS Days,
    DIV(TO_INT(Days),86400) As [Days (Old)],
    EXTRACT_PATH(TO_LOWERCASE(path)) AS ContentPath,
    TO_LOWERCASE(name) AS FileName
FROM 'c:\*.doc' 
WHERE
  (attributes NOT LIKE 'D%')
AND
  ([Days (Old)] >= TO_INT('0'))
ORDER BY [Creation Date Time] DESC
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321