CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2002
    Posts
    28

    Problem with CFindFile

    How do I get all Files (*.mp3) in this Code:

    BROWSEINFO bi = { 0 };
    TCHAR path[MAX_PATH];
    char mydir[MAX_PATH];

    bi.lpszTitle = _T("Wähle ein Verzeichnis das ausgelesen werden soll!");
    bi.pszDisplayName = path;
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );

    if ( pidl != 0 )
    {
    SHGetPathFromIDList(pidl, mydir); // besorgt den Namen des Ordners
    if (CString(mydir).IsEmpty())
    {
    AfxMessageBox("Wähle ein gültiges Verzeichnis !!!");
    OnOpenDirectory(); //Siehe hierzu Zeile 6 dieses Postings ;-)
    }
    }




    I will open a directory a will all MP3 Files get to a function like TagReadingForFile!
    How does this function work?? Can anybody watch my a code sample??

    Sorry for my bad english, i am german!!


  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Problem with CFindFile

    1. Using API functions

    #include <string>
    #include <vector>
    #include <iostream>

    #include <conio.h>

    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    int SearchDirectory(vector<string> &refvecFiles,
    const string &refcstrRootDirectory,
    const string &refcstrExtension)
    {
    string strFilePath; // Filepath
    string strPattern; // Pattern
    HANDLE hFile; // Handle to directory
    WIN32_FIND_DATA FileInformation; // Directory information


    strPattern = refcstrRootDirectory + "\\*." + refcstrExtension;
    hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
    if(hFile != INVALID_HANDLE_VALUE)
    {
    do
    {
    if(FileInformation.cFileName[0] != '.')
    {
    strFilePath.erase();
    strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

    // Save path
    refvecFiles.push_back(strFilePath);
    }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);

    DWORD dwError = ::GetLastError();
    if(dwError != ERROR_NO_MORE_FILES)
    return dwError;
    }

    return 0;
    }

    int main()
    {
    vector<string> vecFiles;

    // Search 'c:' for '.mp3' files
    int iRC = SearchDirectory(vecFiles, "c:", "mp3");
    if(iRC)
    {
    cout << "Error " << iRC << endl;
    return -1;
    }

    // Print results
    for(vector<string>::iterator iter = vecFiles.begin();
    iter != vecFiles.end();
    ++iter)
    cout << *iter << endl;

    // Wait for keystroke
    _getch();

    return 0;
    }



    2. Using MFC 'CFileFind'

    #include <string>
    #include <vector>
    #include <iostream>

    #include <conio.h>

    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    int SearchDirectory(vector<string> &refvecFiles,
    const string &refcstrRootDirectory,
    const string &refcstrExtension)
    {
    string strFilePath; // Filepath
    string strPattern; // Pattern
    CFileFind FileFinder;
    HANDLE hFile; // Handle to directory
    WIN32_FIND_DATA FileInformation; // Directory information


    strPattern = refcstrRootDirectory + "\\*." + refcstrExtension;

    BOOL fWorking = FileFinder.FindFile(strPattern.c_str());

    while(fWorking)
    {
    fWorking = FileFinder.FindNextFile();

    if(FileFinder.IsDots() == FALSE)
    {
    strFilePath.erase();
    strFilePath = (LPCTSTR) FileFinder.GetFilePath();

    // Save path
    refvecFiles.push_back(strFilePath);
    }
    }

    FileFinder.Close();

    return 0;
    }



    Since I just wrote down the code I didn't test it - it should work though...

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  3. #3
    Join Date
    Jan 2002
    Posts
    28

    Re: Problem with CFindFile

    I use mFC and how must I enter the function, with "add member function" or any other way and how do I work with the function in this OnOpenFile - function?


    void CMP3Locater2002ODBCView::OnOpenFile()
    {
    // TODO: Code für Befehlsbehandlungsroutine hier einfügen
    OPENFILENAME ofn;
    CHAR szFile[_MAX_PATH];

    ZeroMemory(&ofn,sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = m_hWnd;
    ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_EXTENSIONDIFFERENT;
    ofn.nMaxFile = sizeof(szFile);
    *szFile = '\0';
    ofn.lpstrFile = szFile;
    ofn.lpstrTitle = "MP3 Datei Öffnen";
    ofn.lpstrDefExt = "mp3 ";
    ofn.lpstrFilter = "MP3-Datei (*.mp3)\0*.mp3\0";
    ofn.lpstrInitialDir="C:";
    int iResult = GetOpenFileName(&ofn);
    if(iResult)
    {
    MessageBox(ofn.lpstrFile);

    }
    }





  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Problem with CFindFile


    // CMP3Locater2002ODBCView.h
    // Add the necessary includes
    #include <string>
    #include <vector>

    using std::string;
    using std::vector;

    // Add the function declaration in the private section
    private:
    int SearchDirectory(vector<string> &refvecFiles,
    const string &refcstrRootDirectory,
    const string &refcstrExtension);


    // CMP3Locater2002ODBCView.cpp
    // Add the function
    int CMP3Locater2002ODBCView::SearchDirectory(vector<string> &refvecFiles,
    const string &refcstrRootDirectory,
    const string &refcstrExtension)
    {
    string strFilePath; // Filepath
    string strPattern; // Pattern
    CFileFind FileFinder;


    strPattern = refcstrRootDirectory + "\\*." + refcstrExtension;

    BOOL fWorking = FileFinder.FindFile(strPattern.c_str());

    while(fWorking)
    {
    fWorking = FileFinder.FindNextFile();

    if(FileFinder.IsDots() == FALSE)
    {
    strFilePath.erase();
    strFilePath = (LPCTSTR) FileFinder.GetFilePath();

    // Save path
    refvecFiles.push_back(strFilePath);
    }
    }

    FileFinder.Close();

    return 0;
    }

    // Add the function within your 'OnOpenFile()'
    void CMP3Locater2002ODBCView::OnOpenFile()
    {
    // TODO: Code für Befehlsbehandlungsroutine hier einfügen
    OPENFILENAME ofn;
    CHAR szFile[_MAX_PATH];

    ZeroMemory(&ofn,sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = m_hWnd;
    ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_EXTENSIONDIFFERENT;
    ofn.nMaxFile = sizeof(szFile);
    *szFile = '\0';
    ofn.lpstrFile = szFile;
    ofn.lpstrTitle = "MP3 Datei Öffnen";
    ofn.lpstrDefExt = "mp3";
    ofn.lpstrFilter = "MP3-Datei (*.mp3)\0*.mp3\0";
    ofn.lpstrInitialDir="C:";

    int iResult = GetOpenFileName(&ofn);
    if(iResult)
    {
    MessageBox(ofn.lpstrFile);

    vector<string> vecFiles;
    char szDirectory[_MAX_PATH] = "";

    memset(szDirectory, 0, sizeof(szDirectory));

    // Extract directory
    strncpy(szDirectory, szFile, strrchr(szFile, "\\") - szFile);

    // Search directory
    int iRC = SearchDirectory(vecFiles, szDirectory, "mp3");
    if(iRC)
    MessageBox("Error searching directory");
    else
    // Do something with all found .mp3-files
    }
    }



    I didn't test it... ;-)

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  5. #5
    Join Date
    Jan 2002
    Posts
    28

    Re: Problem with CFindFile

    There is only 1 problem with


    strncpy(szDirectory, szFile, strchr(szFile, "\\") - szFile);




    the Compiler error is:
    C:\MP3Locater2002ODBC\MP3Locater2002ODBCView.cpp(295) : error C2664: 'strchr' : Konvertierung des Parameters 2 von 'char [2]' in 'int' nicht moeglich
    Diese Konvertierung erfordert einen reinterpret_cast-Operator oder eine Typumwandlung im C- oder Funktionsformat

    hope you can help me!!

    Greats Andreas


  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Problem with CFindFile

    Sorry my fault...

    strncpy(szDirectory, szFile, strrchr(szFile, '\\') - szFile);




    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  7. #7
    Join Date
    Jan 2002
    Posts
    28

    Re: Problem with CFindFile

    Sorry I made a fault!! i will open with this code the directory but I can only read the first file! The second prob is that I can't open every file for reading the ID3 Tag! The function to read Tags is "TagReadingForFile" it will works when I open a file!! Hope you can help me again!!


    void CMP3Locater2002ODBCView::OnOpenDirectory()
    {
    // TODO: Code für Befehlsbehandlungsroutine hier einfügen
    BROWSEINFO bi = { 0 };
    TCHAR path[MAX_PATH];
    char mydir[MAX_PATH];

    bi.lpszTitle = _T("Wähle ein Verzeichnis das ausgelesen werden soll!");
    bi.pszDisplayName = path;
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );

    if ( pidl != 0 )
    {
    SHGetPathFromIDList(pidl, mydir); // besorgt den Namen des Ordners
    if (CString(mydir).IsEmpty())
    {
    AfxMessageBox("Wähle ein gültiges Verzeichnis !!!");
    OnOpenDirectory(); //Siehe hierzu Zeile 6 dieses Postings ;-)
    }
    }
    //Dateien öffnen nur MP3 bis jetzt möglich!!!!!

    vector<string> vecFiles;
    char szDirectory[_MAX_PATH] = "";
    memset(szDirectory, 0,sizeof(szDirectory));
    // Extract directory
    strncpy(szDirectory, mydir, strrchr(mydir, '\\') - mydir);
    // Search directory
    int iRC = SearchDirectory(vecFiles, szDirectory, "mp3");
    if(iRC)
    MessageBox("Error searching directory");
    else
    TagReadingForFile(mydir);
    OnRecordNew();
    }





  8. #8
    Join Date
    Jan 2002
    Posts
    28

    Re: Problem with CFindFile

    Which parameter must I give to the function by this code by using the mfc function??

    void CMP3Locater2002ODBCView::OnOpenDirectory()
    {
    // TODO: Code für Befehlsbehandlungsroutine hier einfügen
    BROWSEINFO bi = { 0 };
    TCHAR path[MAX_PATH];
    char mydir[MAX_PATH];

    bi.lpszTitle = _T("Wähle ein Verzeichnis das ausgelesen werden soll!");
    bi.pszDisplayName = path;
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );

    if ( pidl != 0 )
    {
    SHGetPathFromIDList(pidl, mydir); // besorgt den Namen des Ordners
    if (CString(mydir).IsEmpty())
    {
    AfxMessageBox("Wähle ein gültiges Verzeichnis !!!");
    OnOpenDirectory(); //Siehe hierzu Zeile 6 dieses Postings ;-)
    }
    }
    //Dateien öffnen nur MP3 bis jetzt möglich!!!!!
    SearchDirectory();
    TagReadingForFile();
    OnRecordNew;
    }




    Thanks andreas


  9. #9
    Join Date
    Jan 2002
    Posts
    28

    Re: Problem with CFindFile

    Which parameter must I give to the function by this code by using the mfc function??


    void CMP3Locater2002ODBCView::OnOpenDirectory()
    {
    // TODO: Code für Befehlsbehandlungsroutine hier einfügen
    BROWSEINFO bi = { 0 };
    TCHAR path[MAX_PATH];
    char mydir[MAX_PATH];

    bi.lpszTitle = _T("Wähle ein Verzeichnis das ausgelesen werden soll!");
    bi.pszDisplayName = path;
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );

    if ( pidl != 0 )
    {
    SHGetPathFromIDList(pidl, mydir); // besorgt den Namen des Ordners
    if (CString(mydir).IsEmpty())
    {
    AfxMessageBox("Wähle ein gültiges Verzeichnis !!!");
    OnOpenDirectory(); //Siehe hierzu Zeile 6 dieses Postings ;-)
    }
    }
    //Dateien öffnen nur MP3 bis jetzt möglich!!!!!
    SearchDirectory(/*which Parameters*/);
    TagReadingForFile(/*here I msut enter the File name*/);
    OnRecordNew();
    }





    must i make a for.. do ... function to open every file next by next???

    Greats Andreas


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured