Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo.getFiles beginning with

I've come across some strange behavior trying to get files that start with a certain string.

Please would someone give a working example on this:

I want to get all files in a directory that begin with a certain string, but also contain the xml extension.

for example:

 apples_01.xml
 apples_02.xml
 pears_03.xml

I want to be able to get the files that begin with apples.

So far I have this code

 DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath);
 FileInfo[] taskFiles = taskDirectory.GetFiles("*.xml");
like image 716
JL. Avatar asked Jul 29 '09 08:07

JL.


People also ask

What does directory GetFiles return?

GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

What is the use of the public function GetFiles as FileInfo()?

Public Function GetFiles As FileInfo()Returns a file list from the current directory. For complete list of properties and methods please visit Microsoft's documentation.

How do I get the filename from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.


2 Answers

FileInfo[] taskFiles = taskDirectory.GetFiles("apples*.xml");
like image 124
Cerebrus Avatar answered Oct 20 '22 21:10

Cerebrus


var taskFiles = taskDirectory.GetFiles("*.xml").Where(p => p.Name.StartsWith("apples"));
like image 44
CodeSpeaker Avatar answered Oct 20 '22 22:10

CodeSpeaker