how to get the file with certain name in a directory which contains many files

I have to open a single file(to read data from it)  from a directory which hundreds of files which contains certain character like  "new" how to do that ?
panthiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
  FileStream^ fs = File::OpenRead( pathOfIleGoesHere );
panthiAuthor Commented:
i need to open the file(from a directory with many files)  which contains the name new in the file name? can you please give code example?
directory="c:\test\"
no of files in it are 100 and one of the file name is
"te****_new.csv" how do i pick that up
thanks for your help
wdosanjosCommented:
Please try:

String^ directory="c:\test\";
array<String^>^files = Directory::GetFiles( directory, "te*_new.csv" ); // or "te????_new.csv", if exactly 4 chars

if (files->Length > 0)
{
	FileStream^ fs = File::OpenRead( files[0] );

	// read the file here
}

Open in new window

sr75Commented:
Here is how you can get all the files in a particular directory.   You would just need some logic on what to do with the name of the file (i.e. something to compare it to)
On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\test"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
	Wscript.Echo objFile.Name
Next

Open in new window

AndyAinscowFreelance programmer / ConsultantCommented:
Extract from the help files about Directory::GetFiles (with a minor modification)

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      
      // Only get files that begin with the letter "c."
      array<String^>^dirs = Directory::GetFiles( "c:\\Mydir\\MySubdir\\", "*new*.*" );  //Find all files with 'new' somewhere in the file name
      Console::WriteLine( "The number of files starting with c is {0}.", dirs->Length );
      Collections::IEnumerator^ myEnum = dirs->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Console::WriteLine( myEnum->Current );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}

Open in new window



Note you need a path with double backslashes: c:\\Mydir\\MySubdir\\ because a single backslash instructs the next character is a special character.

Once you identify which file you want to read from then
FileStream^ fs = File::OpenRead( pathOfFileGoesHere );
can be used to open a filestream to read it - look at the OpenRead method in help for some example code

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.