CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    16

    (Beginner question) Looping thru folders and environment folders

    I am a newer C++ user and very green to the win32 API. I have been looking at the MSDN ref site under "File Management Functions" but don't see a clear function for loading a directory. In my case I need to loop thru the files in a folder to check if the file I need is there or if I should create a new one. Reading and writing files I get, just not how to loop threw a folder. A simple practical example or a web site is welcome. Google has failed me or I'm not searching for the right keywords.

    Second I'm not sure if this is a broad question but how do you detect "environment " folders like the users "my documents" folder or the "Program files" folder. I assume it might vary a cross the different versions of windows but my Google attempts were fruitless. Practice examples or a web link would be very welcome.

    -Ryan

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: (Beginner question) Looping thru folders and environment folders

    If you only need to check if a file exits you can use the PathFileExists API.
    If you want to search through a folder and all of its sub-folders you would need a recursive function using the FindFirstFile/FindNextFile APIs.

    To get the path of special folders, use the SHGetFolderPath API.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Jul 2007
    Posts
    16

    Re: (Beginner question) Looping thru folders and environment folders

    Thanks thats what I was looking for.

    What is the correct way to define the WINAPI scope?

    I am trying this, its prob not correct but that mute since the functions dont fall in scope, is it as simple as something like WINAPI::CSIDL_MYPICTURES?

    Code:
    #include <string>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    TCHAR szPath[MAX_PATH];
    
    int main(int argc, char *argv[])
    {
    	SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);  //compile error: CSIDL_MYPICTURES was not declared in this scope
    	cout << "path: " << szPath << endl;
    	return 0;
    }
    Last edited by ryanmills; June 5th, 2009 at 01:53 PM. Reason: typo

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: (Beginner question) Looping thru folders and environment folders

    Quote Originally Posted by ryanmills View Post
    Thanks thats what I was looking for.

    What is the correct way to define the WINAPI scope?
    The Windows API is a 'C' based library. Therefore there is no such thing as namespaces in the Windows API. Those are #defined constants.

    Did you include <shlwapi.h>?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2007
    Posts
    16

    Re: (Beginner question) Looping thru folders and environment folders

    Thanks, I guess my mistake was assuming the error was related in the way not defining iostream before using cout spits and error. But change it to std scope (ie std:cout) and your fine. Anyway thanks, so much to learn.

    -Ryan

  6. #6
    Join Date
    Jul 2007
    Posts
    16

    Re: (Beginner question) Looping thru folders and environment folders

    I have a simple function to loop thru the files but for some reason I am only listing the folder its self. I'm sure this is not the best way to do this but does anything jump out as wrong with this. The goal is just to list the file names in the "My Pictures" folder.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <shlwapi.h>
    
    TCHAR szPath[MAX_PATH];
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError=0;
    
    int main(int argc, char *argv[])
    {
    	
    	SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);
    	
    	std::cout << "searching: " << szPath << std::endl << std::endl;
    	
    	hFind = FindFirstFile(szPath, &ffd);
    
    	if (INVALID_HANDLE_VALUE == hFind) 
    	{
    		std::cout << "folder not found" << std::endl;
    	} 
    
       do
       {
    
    		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			std::cout << "dir: " <<ffd.cFileName << std::endl;
    		}
    		else
    		{
    			filesize.LowPart = ffd.nFileSizeLow;
    			filesize.HighPart = ffd.nFileSizeHigh;
    			std::cout << "file: " << ffd.cFileName << " size: " << filesize.QuadPart << std::endl;
    		}
    	}
    	while (FindNextFile(hFind, &ffd) != 0);
       
    	return 0;
    }
    
    // returns
    //searching: \\PDC1\Users\me\My Documents\My Pictures
    
    //dir: My Pictures
    The SHGetFolderPath call is returning \\PDC1\Users\me\My Documents\My Pictures however when I point FindFirstFile to that folder it loops once on that folder and only returns that folder. When it should be returning the files inside the folder, even if it was returning the folder below it there should have been more folders but its only seeing that folder. Any advice is welcome.

  7. #7
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: (Beginner question) Looping thru folders and environment folders

    Quote Originally Posted by ryanmills View Post
    to check if the file I need is there or if I should create a new one
    You can specify flags in the CreateFile function to create a new file for you..

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