Find a file in a directory

This is a discussion on Find a file in a directory within the C++ Programming forums, part of the General Programming Boards category; I am trying to FindFirstFile() in a selected path using folderBrowserDialog1. I have wrote "C:\*.*" as a test and get ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Find a file in a directory

    I am trying to FindFirstFile() in a selected path using folderBrowserDialog1.


    I have wrote "C:\\*.*" as a test and get this compile error: Later I beleive I will put this as my Directory as I use a
    BrowserDialog: ("folderBrowserDialog1->SelectedPath;" ?)

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

    Any Idéas of why this is happening ?
    Code:
    folderBrowserDialog1->ShowDialog();
    
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    TCHAR DirSpec[100];     
    
    // My directory will be: folderBrowserDialog1->SelectedPath;
    
    
    hFind = FindFirstFile("C:\\*.*" , &FindFileData);
    Last edited by Coding; 02-10-2008 at 05:26 PM.

  2. #2
    coder
    Join Date
    Feb 2008
    Posts
    127
    The compiler already tells you what is happening:
    the function FindFirstFile takes a 'LPCWSTR' type as first parameter, but you are giving it a 'const char[10]' ("C:\\*.*")

    Try
    hFind = FindFirstFile(folderBrowserDialog1->SelectedPath, &FindFileData);

    and see what happens
    Last edited by carlorfeo; 02-10-2008 at 05:32 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I tried to do this line to wich should be my approach then.
    Code:
    hFind = FindFirstFile(folderBrowserDialog1->SelectedPath, &FindFileData);
    The Compiler Error look like this, now it is a System::String^ instead.
    error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'System::String ^' to 'LPCWSTR'

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    So I guess you need to know what an LPCWSTR is.
    I suggest you to google that, there are many threads about problems like yours

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes okay, I will try to do that and see what I can come up with.. Thank you...

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I did found out a solution on this. I converted from string to LPCWSTR like this.


    The code look like this and do compile:
    Code:
    folderBrowserDialog1->ShowDialog();
    			
    std::string path = "c:\\MyDirectory\\*.txt";
    LPCWSTR Fold = L"c:\\MyDirectory\\*.txt";
    
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = FindFirstFile(Fold, &FindFileData);
    			
            if( hFind != INVALID_HANDLE_VALUE)
                
            {
    
                     do 
                     {
                              // save this file name
                     } while( FindNextFile(hFind, &FindFileData) );
                        
                         FindClose(hFind);
            }
    Last edited by Coding; 02-10-2008 at 08:13 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    And once again, you seem to be using C++/CLI.
    These things are tricky. There should be some way to get a raw character array from System::String, but I don't know how.
    This is also called interop, to switch between native and managed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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 and this is that I want to display the Whole Directory inluding the found .txt files that is in that choosen directory that I have browsed to.
    In order to do this I beleive I have to use:
    folderBrowserDialog1->SelectedPath in any way.

    Code:
    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);
    	}
    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. I tried this but this ofcourse didn´t work:
    Code:
    std::string Folder = folderBrowserDialog1->SelectedPath;
    Last edited by Coding; 02-11-2008 at 06:04 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Coding View Post
    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. I tried this but this ofcourse didn´t work:
    Code:
    std::string Folder = folderBrowserDialog1->SelectedPath;
    I would be easier to go managed all the way. There are managed functions finding files in directories.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Okay, is it possible in the version I am doing now to do it in a managed way you meen.
    Another approach ?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The .NET Framework contains thousands (or so) of functions available for your use. There is pretty much no need to use anything else! Of course there's a way to find files in a directory through the framework, and yes, that means it's managed code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Do you have any function/approach in mind that you know about as this one is the only one I know about as it is the first time I find files in directories.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    No, I don't so managed, so I have no clue what to use. But it isn't difficult to find. Crawl msdn a little and you'll be likely to find something.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I will do that and see what I can find out. It was not the easiest way as you said to do the function I am trying out now. Thanks Elysia...

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I am trying to convert a System::String ^ to LPCSTR.

    My System::String look like this:
    System::String ^ files8 = folderBrowserDialog1->SelectedPath->ToString();

    So what I want to do is to convert files8 to a LPCSTR wich I have understand is a const *char but it seems that I cant find any good example anywhere how to do it or any code to start with.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Help Required!!!!!
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 11-27-2005, 02:56 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21