Click to See Complete Forum and Search --> : (Beginner question) Looping thru folders and environment folders


ryanmills
June 4th, 2009, 08:19 PM
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

_Superman_
June 5th, 2009, 12:59 AM
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.

ryanmills
June 5th, 2009, 02:48 PM
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?


#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;
}

Paul McKenzie
June 5th, 2009, 03:05 PM
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

ryanmills
June 5th, 2009, 03:14 PM
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

ryanmills
June 5th, 2009, 03:49 PM
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.


#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.

wigga
June 9th, 2009, 07:09 PM
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..