I am using this code inside a button. When clicking the button I browerDialog appears.
I can now browe to a folder and press OK. The Directory will be inserted in the textBox1.

What I now want to do is this:
Inside of this Folder I have a .txt File. I want this name to appear in the textBox instead.
I know that I have to use FindFirstFile() for this but I really dont know how to start. I have red about 10 examples but cant really understand what my first thing should be to do.

Thank You

folderBrowserDialog1->ShowDialog();
this->textBox1->Text = folderBrowserDialog1->SelectedPath;

I have found some code that should be the beginning to scan a selected directory:

HANDLE hFind;
WIN32_FIND_DATA FindData;
int ErrorCode;
TCHAR Quest[100];

hFind = FindFirstFile(Quest, &FindData);

This code works if I compile it. But what is Quest ? Where will I put the directory to search for the .txt Files ?

I might have come this far. Here I am lost how to do.
I suppose I have to put my Directory specification where "Directory" is but how will I put:
"folderBrowserDialog1->SelectedPath;" as my Directory.
If I write "C:\*.*" as a test where "Directory" is I get compile error:
FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'
'*' : unrecognized character escape sequence

I will be happy for help here.
Thanks in advance !

folderBrowserDialog1->ShowDialog();

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR DirSpec[100];     

// directory specification   
// My directory will be: folderBrowserDialog1->SelectedPath;


hFind = FindFirstFile("Directory", &FindFileData);

        if (hFind != INVALID_HANDLE_VALUE)
{	
        this->textBox1->Text = folderBrowserDialog1->SelectedPath;
}

I suppose I will have to write the path from where I will FindFirstFile() where "Directory" is written in the code but the compiler complains about:

FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'

Any ideas of what this could be ?

Thanks...

I might have come this far. Here I am lost how to do.
I suppose I have to put my Directory specification where "Directory" is but how will I put:
"folderBrowserDialog1->SelectedPath;" as my Directory.
If I write "C:\*.*" as a test where "Directory" is I get compile error:
FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'
'*' : unrecognized character escape sequence

I will be happy for help here.
Thanks in advance !

folderBrowserDialog1->ShowDialog();

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR DirSpec[100];     

// directory specification   
// My directory will be: folderBrowserDialog1->SelectedPath;


hFind = FindFirstFile("Directory", &FindFileData);

        if (hFind != INVALID_HANDLE_VALUE)
{	
        this->textBox1->Text = folderBrowserDialog1->SelectedPath;
}

>>FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'
That errror means you are compiling your program for UNICODE, which is the default setting for VC++ 2005/2008 compilers. If you don't care about languages other than English you can turn UNICODE off.
Project --> Properties --> Configuration Properties --> General. On the right side of the screen change "Character Set" (third item up from the bottom) to "Not Set".

There is an extensive example of how to use FindFirstFile() in the code snippets. But it might be a little too advanced for you. The first parameter is the path and files that you want to start the search. For example if you want all the *.txt files in c:\MyDirectory then the first paramter would be "c:\\MyDirectory\\*.txt". The second parameter is a pointer to your WIN32_FIND_DATA object.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FiundFileData);
if( hFind != INVALID_HANDLE_VALUE)
{
    do {
           // save this file name
    } while( FindNextFile(hFind, &FindFileData) );
    FindClose(hFind);
}

Note that the above will not process any subdirectories unless the directory name has a *.txt extension (which is probably very very unlikely)

Thanks for your reply. I did find the setting to put --> General to "Not Set" and clicked Apply. This will be great becase only English is okay !

I did insert this code then but still I have got the compiler error that look like this:

'FindFirstFileW' : cannot convert parameter 1 from 'const char [21]' to 'LPCWSTR'

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FindFileData);
			
			
if( hFind != INVALID_HANDLE_VALUE)
{
do 
{
           // save this file name
} 
                     while( FindNextFile(hFind, &FindFileData) );
                      FindClose(hFind);
}

>>FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'
That errror means you are compiling your program for UNICODE, which is the default setting for VC++ 2005/2008 compilers. If you don't care about languages other than English you can turn UNICODE off.
Project --> Properties --> Configuration Properties --> General. On the right side of the screen change "Character Set" (third item up from the bottom) to "Not Set".

There is an extensive example of how to use FindFirstFile() in the code snippets. But it might be a little too advanced for you. The first parameter is the path and files that you want to start the search. For example if you want all the *.txt files in c:\MyDirectory then the first paramter would be "c:\\MyDirectory\\*.txt". The second parameter is a pointer to your WIN32_FIND_DATA object.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FiundFileData);
if( hFind != INVALID_HANDLE_VALUE)
{
    do {
           // save this file name
    } while( FindNextFile(hFind, &FindFileData) );
    FindClose(hFind);
}

Note that the above will not process any subdirectories unless the directory name has a *.txt extension (which is probably very very unlikely)

I attached a line and put back the setting ---> General as it were. The line I did attached was:
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";
Now it does compile. I wonder if this could be a right approach ?

Thank You !

std::string path = "c:\\MyDirectory\\*.txt";
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FindFileData);
			
if( hFind != INVALID_HANDLE_VALUE)
{
do 
{
           // save this file name
} 
                     while( FindNextFile(hFind, &FindFileData) );
                      FindClose(hFind);
}

>>FindFirstFileW' : cannot convert parameter 1 from 'const char [6]' to 'LPCWSTR'
That errror means you are compiling your program for UNICODE, which is the default setting for VC++ 2005/2008 compilers. If you don't care about languages other than English you can turn UNICODE off.
Project --> Properties --> Configuration Properties --> General. On the right side of the screen change "Character Set" (third item up from the bottom) to "Not Set".

There is an extensive example of how to use FindFirstFile() in the code snippets. But it might be a little too advanced for you. The first parameter is the path and files that you want to start the search. For example if you want all the *.txt files in c:\MyDirectory then the first paramter would be "c:\\MyDirectory\\*.txt". The second parameter is a pointer to your WIN32_FIND_DATA object.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FiundFileData);
if( hFind != INVALID_HANDLE_VALUE)
{
    do {
           // save this file name
    } while( FindNextFile(hFind, &FindFileData) );
    FindClose(hFind);
}

Note that the above will not process any subdirectories unless the directory name has a *.txt extension (which is probably very very unlikely)

>>I wonder if this could be a right approach ?
It could be, but what you posted isn't because the code you posted doesn't fix a thing when UNICODE is enabled.

>>LPCWSTR berit = L"c:\\MyDirectory\\*.txt";
Why????? that variable is never used in your program. so you may as well delete it.

I did a mistake in my code that I posted. The Variable "berit" is used.
So the complete code that I have got now look like as below.

I believe that I have got the filename in the "printf". This code compiles
but what I want to do is to put the filename in this->textBox1->Text
I beleive I have to convert it to a System::string ^ but really dont know the approach to this as I have searched round a lot.
The code I have now look as follows:

Thanks:

std::string path = "c:\\MyDirectory\\*.txt";
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";

printf ("Target directory is %s.\n", berit);

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(berit, &FindFileData);
			
        if( hFind != INVALID_HANDLE_VALUE)
	{
		do 
            {
		 printf ("First file name is %s.\n", FindFileData.cFileName);
		//this->textBox1->Text = printf;   

	    } 
                                  while( FindNextFile(hFind, &FindFileData) );
                                  FindClose(hFind);
	}

I attached a line and put back the setting ---> General as it were. The line I did attached was:
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";
Now it does compile. I wonder if this could be a right approach ?

Thank You !

std::string path = "c:\\MyDirectory\\*.txt";
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FindFileData);
			
if( hFind != INVALID_HANDLE_VALUE)
{
do 
{
           // save this file name
} 
                     while( FindNextFile(hFind, &FindFileData) );
                      FindClose(hFind);
}

This code is working great. I have come up with a solution to convert from std::string to System::string ^

With this code I open a browser, browse to a directory and click OK. When choosing whatever directory and press OK the code displays the textfile in a textBox that does exist under the directory "c:\\MyDirectory\\*.txt" as seen in the code. Great !

Now is one thing left and this is that I want to display the .txt files that is in the directory that I have browsed to. In order to do this I beleive I have to use:
folderBrowserDialog1->SelectedPath in any way.

Somehow I beleive I have to convert what is selected to just a std::string but dont know how this could be done.
Would be happy for any idéas to this

Thank You...

folderBrowserDialog1->ShowDialog();
							
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind = FindFirstFile("c:\\MyDirectory\\*.txt", &FindFileData);
			
	if( hFind != INVALID_HANDLE_VALUE)
	{
			do 
		{
		
			std::string files = FindFileData.cFileName;
			std::string files2 = files.substr(0);
			System::String ^ files3 = gcnew String(files2.c_str());
			this->textBox1->Text = files3;   

		} 
                  while( FindNextFile(hFind, &FindFileData) );
                  FindClose(hFind);
	}

I did a mistake in my code that I posted. The Variable "berit" is used.
So the complete code that I have got now look like as below.

I believe that I have got the filename in the "printf". This code compiles
but what I want to do is to put the filename in this->textBox1->Text
I beleive I have to convert it to a System::string ^ but really dont know the approach to this as I have searched round a lot.
The code I have now look as follows:

Thanks:

std::string path = "c:\\MyDirectory\\*.txt";
LPCWSTR berit = L"c:\\MyDirectory\\*.txt";

printf ("Target directory is %s.\n", berit);

WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(berit, &FindFileData);
			
        if( hFind != INVALID_HANDLE_VALUE)
	{
		do 
            {
		 printf ("First file name is %s.\n", FindFileData.cFileName);
		//this->textBox1->Text = printf;   

	    } 
                                  while( FindNextFile(hFind, &FindFileData) );
                                  FindClose(hFind);
	}
This article has been dead for over six months. Start a new discussion instead.