Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting folder names

Getting folder names

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashokvishnu
    wrote on last edited by
    #1

    Hi Gurus, I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome. void GetSubFolderNames( LPCTSTR szFolderPath ) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD dwError; hFind = FindFirstFile(szFolderPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { do { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( !lstrcmp( FindFileData.cFileName, L"." ) || !lstrcmp( FindFileData.cFileName, L"..")) { //Do nothing for "." and ".." folders } else { CString fileName = FindFileData.cFileName; printf ("First file name is %s\n", fileName); } } } while (FindNextFile(hFind, &FindFileData) != 0) ; dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); } Thanks

    C++beginer

    D T T D 4 Replies Last reply
    0
    • A ashokvishnu

      Hi Gurus, I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome. void GetSubFolderNames( LPCTSTR szFolderPath ) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD dwError; hFind = FindFirstFile(szFolderPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { do { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( !lstrcmp( FindFileData.cFileName, L"." ) || !lstrcmp( FindFileData.cFileName, L"..")) { //Do nothing for "." and ".." folders } else { CString fileName = FindFileData.cFileName; printf ("First file name is %s\n", fileName); } } } while (FindNextFile(hFind, &FindFileData) != 0) ; dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); } Thanks

      C++beginer

      D Offline
      D Offline
      Don Box
      wrote on last edited by
      #2

      Use CFileFind

      Jubin Almighty braindrain1@rediffmail.com ----------------------------- Come online at:- fitiyal@yahoo.com

      1 Reply Last reply
      0
      • A ashokvishnu

        Hi Gurus, I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome. void GetSubFolderNames( LPCTSTR szFolderPath ) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD dwError; hFind = FindFirstFile(szFolderPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { do { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( !lstrcmp( FindFileData.cFileName, L"." ) || !lstrcmp( FindFileData.cFileName, L"..")) { //Do nothing for "." and ".." folders } else { CString fileName = FindFileData.cFileName; printf ("First file name is %s\n", fileName); } } } while (FindNextFile(hFind, &FindFileData) != 0) ; dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); } Thanks

        C++beginer

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        SADirRead[^] class is perfect for what you need...


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        1 Reply Last reply
        0
        • A ashokvishnu

          Hi Gurus, I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome. void GetSubFolderNames( LPCTSTR szFolderPath ) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD dwError; hFind = FindFirstFile(szFolderPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { do { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( !lstrcmp( FindFileData.cFileName, L"." ) || !lstrcmp( FindFileData.cFileName, L"..")) { //Do nothing for "." and ".." folders } else { CString fileName = FindFileData.cFileName; printf ("First file name is %s\n", fileName); } } } while (FindNextFile(hFind, &FindFileData) != 0) ; dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); } Thanks

          C++beginer

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          ashokvishnu wrote:

          CString fileName = FindFileData.cFileName;

          strange you getting problem here! try CString construtor!

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          1 Reply Last reply
          0
          • A ashokvishnu

            Hi Gurus, I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome. void GetSubFolderNames( LPCTSTR szFolderPath ) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD dwError; hFind = FindFirstFile(szFolderPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { do { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( !lstrcmp( FindFileData.cFileName, L"." ) || !lstrcmp( FindFileData.cFileName, L"..")) { //Do nothing for "." and ".." folders } else { CString fileName = FindFileData.cFileName; printf ("First file name is %s\n", fileName); } } } while (FindNextFile(hFind, &FindFileData) != 0) ; dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); } Thanks

            C++beginer

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            So are you using Unicode or not? If so, then try:

            printf("First file name is %S\n", (LPCTSTR) fileName);


            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

            "Judge not by the eye but by the heart." - Native American Proverb

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups