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

Posted on 2011-10-05
Last Modified: 2014-08-01
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 ?
Question by:panthi
    LVL 43

    Expert Comment

      FileStream^ fs = File::OpenRead( pathOfIleGoesHere );

    Author Comment

    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?
    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
    LVL 23

    Expert Comment

    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

    LVL 15

    Expert Comment

    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

    Open in new window

    LVL 43

    Accepted Solution

    Extract from the help files about Directory::GetFiles (with a minor modification)

    using namespace System;
    using namespace System::IO;
    int main()
          // 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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now