Reading Subdirectories with win32 - c++

I'm trying to read some sub-directories with win32 functions and it looks like this. Most everything works fine. I haven't got to run the function completely because I'm still debugging it. My problem: I have 5 actual files and two sub-directories. When I try to grab the file names of each subdirectory and file in the directory I get this: ".", "..", "Subdirectory1", "Subdirectory", "rest of the files"... Why did I get a period, two periods, and then the actual files in the folder?
static std::vector<std::string> ReadAllFilesIntoArray(std::string contentDirPath, std::string fileType)
{
std::vector<std::string> filePaths;
std::wstring strTemp;
strTemp.assign(contentDirPath.begin(), contentDirPath.end());
HANDLE hFile = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA FindFileData;
hFile = FindFirstFile(strTemp.c_str(), &FindFileData);
if (INVALID_HANDLE_VALUE != hFile)
{
int i = 0;
do{
// If it's a directory
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
// Convert wchar[260] -> std::string
char ch[260];
char DefChar = ' ';
WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, ch, 260, &DefChar, NULL);
std::string ss(ch);
std::vector<std::string> localFilePaths = ReadAllFilesIntoArray(contentDirPath.assign(contentDirPath.begin(), contentDirPath.end() - 5) + ss + "//*", fileType);
// Append the file paths found in the subdirectory to the ones found in the current directory
filePaths.insert(filePaths.begin(), localFilePaths.begin(), localFilePaths.end());
}
// Convert wchar[260] -> std::string
char ch[260];
char DefChar = ' ';
WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, ch, 260, &DefChar, NULL);
std::string tempString(ch);
// Then add to list if it's equal to the file type we are checking for
if (tempString.substr(tempString.size() - 3, tempString.size()) == fileType)
{
filePaths.resize(i + 1);
filePaths[i] = ch;
i++;
}
} while (FindNextFile(hFile, &FindFileData));
FindClose(hFile);
}
return filePaths;
}

They are special names that represent the current directory (.) and the parent directory (..). Enumeration code is typically written with special case checks to ignore these two special values.

Related

Iterate directory or files with wildcards in c++ [duplicate]

how can I easy get all files paths from path containing a wildcards? For example: C:/Data*Set/Files*/*.txt and I wrote it on Linux using glob function but I can't do it on Windows :/
FindFirstFile unfortunately doesn't support wildcards in directory names.
I think there should be a Windows solution available but I can't find it.
So you should do away with using OS specific file access, in favor of the OS independent: Filesystem Library
Let's say that you're given filesystem::path input which contains the path with wildcards. To use this to solve your problem you'd need to:
Use parent_path to break apart input into directories
Use filename to obtain the input filename
Obtain a directory_iterator to the relative or absolute path where the input begins
Create a recursive function which takes in begin and end iterators to the obtained parent path, the directory iterator, and the filename
Any time a directory or filename uses a '*' use a regex with the iterator to determine the directory which should be progressed to next
Either return the path to the matching file or an empty path
Due to the excellent Ben Voigt's comment I've updated the algorithm to step over unwildcarded directories.
For example:
regex GenerateRegex(string& arg) {
for (auto i = arg.find('*'); i != string::npos; i = arg.find('*', i + 2)) {
arg.insert(i, 1, '.');
}
return regex(arg);
}
filesystem::path FindFirstFile(filesystem::path directory, filesystem::path::const_iterator& start, const filesystem::path::const_iterator& finish, string& filename) {
while (start != finish && start->string().find('*') == string::npos) {
directory /= *start++;
}
filesystem::directory_iterator it(directory);
filesystem::path result;
if (it != filesystem::directory_iterator()) {
if (start == finish) {
for (auto i = filename.find('.'); i != string::npos; i = filename.find('.', i + 2)) {
filename.insert(i, 1, '\\');
}
const auto re = GenerateRegex(filename);
do {
if (!filesystem::is_directory(it->status()) && regex_match(it->path().string(), re)) {
result = *it;
break;
}
} while (++it != filesystem::directory_iterator());
}
else {
const auto re = GenerateRegex(start->string());
do {
if (it->is_directory() && regex_match(prev(it->path().end())->string(), re)) {
result = FindFirstFile(it->path(), next(start), finish, filename);
if (!result.empty()) {
break;
}
}
} while (++it != filesystem::directory_iterator());
}
}
return result;
}
Which can be called with:
const filesystem::path input("C:/Test/Data*Set/Files*/*.txt");
if (input.is_absolute()) {
const auto relative_parent = input.parent_path().relative_path();
cout << FindFirstFile(input.root_path(), begin(relative_parent), end(relative_parent), input.filename().string()) << endl;
} else {
const auto parent = input.parent_path();
cout << FindFirstFile(filesystem::current_path(), begin(parent), end(parent), input.filename().string()) << endl;
}
Live Example
need understand how FindFirstFile[Ex] work. this is shell over NtQueryDirectoryFile. FindFirstFile[Ex] need divide input name to folder name (which will be opened in used as FileHandle) and search mask used as FileName. mask can be only in file name. folder must have exact name without wildcard to opened first.
as result FindFirstFile[Ex] always open concrete single folder and search in this folder by mask. for recursive search files - we need recursive call FindFirstFile[Ex]. simply usual we use the same constant search mask on all levels. for example when we want find all files begin from X:\SomeFolder we first call FindFirstFile[Ex] with X:\SomeFolder\* on level 0. if we found SomeSubfolder - we call FindFirstFile[Ex] with X:\SomeFolder\SomeSubfolder\* on level 1 and so on. but we can use different search masks on different levels. Data*Set on level 0, Files* on level 1, *.txt on level 2
so we need call FindFirstFileEx recursive and on different recursions level use different masks. for example we want found c:\Program*\*\*.txt. we need start from c:\Program*, then for every founded result append \* mask, then append \*.txt on next level. or we can for example want next - search files by next mask - c:\Program Files*\Internet Explorer\* with any deep level. we can use constant deep search folder mask (optional) with final mask (also optional) used already on all more deep levels.
all this can be really not so hard and efficient implemented:
struct ENUM_CONTEXT : WIN32_FIND_DATA
{
PCWSTR _szMask;
PCWSTR *_pszMask;
ULONG _MaskCount;
ULONG _MaxLevel;
ULONG _nFiles;
ULONG _nFolders;
WCHAR _FileName[MAXSHORT + 1];
void StartEnum(PCWSTR pcszRoot, PCWSTR pszMask[], ULONG MaskCount, PCWSTR szMask, ULONG MaxLevel, PSTR prefix)
{
SIZE_T len = wcslen(pcszRoot);
if (len < RTL_NUMBER_OF(_FileName))
{
memcpy(_FileName, pcszRoot, len * sizeof(WCHAR));
_szMask = szMask, _pszMask = pszMask, _MaskCount = MaskCount;
_MaxLevel = szMask ? MaxLevel : MaskCount;
_nFolders = 0, _nFolders = 0;
Enum(_FileName + len, 0, prefix);
}
}
void Enum(PWSTR pszEnd, ULONG nLevel, PSTR prefix);
};
void ENUM_CONTEXT::Enum(PWSTR pszEnd, ULONG nLevel, PSTR prefix)
{
if (nLevel > _MaxLevel)
{
return ;
}
PCWSTR lpFileName = _FileName;
SIZE_T cb = lpFileName + RTL_NUMBER_OF(_FileName) - pszEnd;
PCWSTR szMask = nLevel < _MaskCount ? _pszMask[nLevel] : _szMask;
SIZE_T cchMask = wcslen(szMask) + 1;
if (cb < cchMask + 1)
{
return ;
}
*pszEnd++ = L'\\', cb--;
DbgPrint("%s[<%.*S>]\n", prefix, pszEnd - lpFileName, lpFileName);
memcpy(pszEnd, szMask, cchMask * sizeof(WCHAR));
ULONG dwError;
HANDLE hFindFile = FindFirstFileEx(lpFileName, FindExInfoBasic, this, FindExSearchNameMatch, 0, FIND_FIRST_EX_LARGE_FETCH);
if (hFindFile != INVALID_HANDLE_VALUE)
{
PWSTR FileName = cFileName;
do
{
SIZE_T FileNameLength = wcslen(FileName);
switch (FileNameLength)
{
case 2:
if (FileName[1] != '.') break;
case 1:
if (FileName[0] == '.') continue;
}
if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_nFolders++;
if (!(dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
{
if (cb < FileNameLength)
{
__debugbreak();
}
else
{
memcpy(pszEnd, FileName, FileNameLength * sizeof(WCHAR));
Enum(pszEnd + FileNameLength, nLevel + 1, prefix - 1);
}
}
}
else if (nLevel >= _MaskCount || (!_szMask && nLevel == _MaskCount - 1))
{
_nFiles++;
DbgPrint("%s%u%u <%.*S>\n", prefix, nFileSizeLow, nFileSizeHigh, FileNameLength, FileName);
}
} while (FindNextFile(hFindFile, this));
if ((dwError = GetLastError()) == ERROR_NO_MORE_FILES)
{
dwError = NOERROR;
}
FindClose(hFindFile);
}
else
{
dwError = GetLastError();
}
if (dwError && dwError != ERROR_FILE_NOT_FOUND)
{
DbgPrint("%s[<%.*S>] err = %u\n", prefix, pszEnd - lpFileName, lpFileName, dwError);
}
}
void Test(PCWSTR pcszRoot)
{
char prefix[MAXUCHAR + 1];
memset(prefix, '\t', RTL_NUMBER_OF(prefix) - 1);
prefix[RTL_NUMBER_OF(prefix) - 1] = 0;
ENUM_CONTEXT ectx;
static PCWSTR Masks[] = { L"Program*", L"*", L"*.txt" };
static PCWSTR Masks2[] = { L"Program*", L"*" };
static PCWSTR Masks3[] = { L"Program Files*", L"Internet Explorer" };
// search Program*\*\*.txt with fixed deep level
ectx.StartEnum(pcszRoot, Masks, RTL_NUMBER_OF(Masks), 0, RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
// search *.txt files from Program*\*\ - any deep level
ectx.StartEnum(pcszRoot, Masks2, RTL_NUMBER_OF(Masks2), L"*.txt", RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
// search all files (*) from Program Files*\Internet Explorer\
ectx.StartEnum(pcszRoot, Masks3, RTL_NUMBER_OF(Masks3), L"*", RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
}

Files in directory with wildcard on Windows

how can I easy get all files paths from path containing a wildcards? For example: C:/Data*Set/Files*/*.txt and I wrote it on Linux using glob function but I can't do it on Windows :/
FindFirstFile unfortunately doesn't support wildcards in directory names.
I think there should be a Windows solution available but I can't find it.
So you should do away with using OS specific file access, in favor of the OS independent: Filesystem Library
Let's say that you're given filesystem::path input which contains the path with wildcards. To use this to solve your problem you'd need to:
Use parent_path to break apart input into directories
Use filename to obtain the input filename
Obtain a directory_iterator to the relative or absolute path where the input begins
Create a recursive function which takes in begin and end iterators to the obtained parent path, the directory iterator, and the filename
Any time a directory or filename uses a '*' use a regex with the iterator to determine the directory which should be progressed to next
Either return the path to the matching file or an empty path
Due to the excellent Ben Voigt's comment I've updated the algorithm to step over unwildcarded directories.
For example:
regex GenerateRegex(string& arg) {
for (auto i = arg.find('*'); i != string::npos; i = arg.find('*', i + 2)) {
arg.insert(i, 1, '.');
}
return regex(arg);
}
filesystem::path FindFirstFile(filesystem::path directory, filesystem::path::const_iterator& start, const filesystem::path::const_iterator& finish, string& filename) {
while (start != finish && start->string().find('*') == string::npos) {
directory /= *start++;
}
filesystem::directory_iterator it(directory);
filesystem::path result;
if (it != filesystem::directory_iterator()) {
if (start == finish) {
for (auto i = filename.find('.'); i != string::npos; i = filename.find('.', i + 2)) {
filename.insert(i, 1, '\\');
}
const auto re = GenerateRegex(filename);
do {
if (!filesystem::is_directory(it->status()) && regex_match(it->path().string(), re)) {
result = *it;
break;
}
} while (++it != filesystem::directory_iterator());
}
else {
const auto re = GenerateRegex(start->string());
do {
if (it->is_directory() && regex_match(prev(it->path().end())->string(), re)) {
result = FindFirstFile(it->path(), next(start), finish, filename);
if (!result.empty()) {
break;
}
}
} while (++it != filesystem::directory_iterator());
}
}
return result;
}
Which can be called with:
const filesystem::path input("C:/Test/Data*Set/Files*/*.txt");
if (input.is_absolute()) {
const auto relative_parent = input.parent_path().relative_path();
cout << FindFirstFile(input.root_path(), begin(relative_parent), end(relative_parent), input.filename().string()) << endl;
} else {
const auto parent = input.parent_path();
cout << FindFirstFile(filesystem::current_path(), begin(parent), end(parent), input.filename().string()) << endl;
}
Live Example
need understand how FindFirstFile[Ex] work. this is shell over NtQueryDirectoryFile. FindFirstFile[Ex] need divide input name to folder name (which will be opened in used as FileHandle) and search mask used as FileName. mask can be only in file name. folder must have exact name without wildcard to opened first.
as result FindFirstFile[Ex] always open concrete single folder and search in this folder by mask. for recursive search files - we need recursive call FindFirstFile[Ex]. simply usual we use the same constant search mask on all levels. for example when we want find all files begin from X:\SomeFolder we first call FindFirstFile[Ex] with X:\SomeFolder\* on level 0. if we found SomeSubfolder - we call FindFirstFile[Ex] with X:\SomeFolder\SomeSubfolder\* on level 1 and so on. but we can use different search masks on different levels. Data*Set on level 0, Files* on level 1, *.txt on level 2
so we need call FindFirstFileEx recursive and on different recursions level use different masks. for example we want found c:\Program*\*\*.txt. we need start from c:\Program*, then for every founded result append \* mask, then append \*.txt on next level. or we can for example want next - search files by next mask - c:\Program Files*\Internet Explorer\* with any deep level. we can use constant deep search folder mask (optional) with final mask (also optional) used already on all more deep levels.
all this can be really not so hard and efficient implemented:
struct ENUM_CONTEXT : WIN32_FIND_DATA
{
PCWSTR _szMask;
PCWSTR *_pszMask;
ULONG _MaskCount;
ULONG _MaxLevel;
ULONG _nFiles;
ULONG _nFolders;
WCHAR _FileName[MAXSHORT + 1];
void StartEnum(PCWSTR pcszRoot, PCWSTR pszMask[], ULONG MaskCount, PCWSTR szMask, ULONG MaxLevel, PSTR prefix)
{
SIZE_T len = wcslen(pcszRoot);
if (len < RTL_NUMBER_OF(_FileName))
{
memcpy(_FileName, pcszRoot, len * sizeof(WCHAR));
_szMask = szMask, _pszMask = pszMask, _MaskCount = MaskCount;
_MaxLevel = szMask ? MaxLevel : MaskCount;
_nFolders = 0, _nFolders = 0;
Enum(_FileName + len, 0, prefix);
}
}
void Enum(PWSTR pszEnd, ULONG nLevel, PSTR prefix);
};
void ENUM_CONTEXT::Enum(PWSTR pszEnd, ULONG nLevel, PSTR prefix)
{
if (nLevel > _MaxLevel)
{
return ;
}
PCWSTR lpFileName = _FileName;
SIZE_T cb = lpFileName + RTL_NUMBER_OF(_FileName) - pszEnd;
PCWSTR szMask = nLevel < _MaskCount ? _pszMask[nLevel] : _szMask;
SIZE_T cchMask = wcslen(szMask) + 1;
if (cb < cchMask + 1)
{
return ;
}
*pszEnd++ = L'\\', cb--;
DbgPrint("%s[<%.*S>]\n", prefix, pszEnd - lpFileName, lpFileName);
memcpy(pszEnd, szMask, cchMask * sizeof(WCHAR));
ULONG dwError;
HANDLE hFindFile = FindFirstFileEx(lpFileName, FindExInfoBasic, this, FindExSearchNameMatch, 0, FIND_FIRST_EX_LARGE_FETCH);
if (hFindFile != INVALID_HANDLE_VALUE)
{
PWSTR FileName = cFileName;
do
{
SIZE_T FileNameLength = wcslen(FileName);
switch (FileNameLength)
{
case 2:
if (FileName[1] != '.') break;
case 1:
if (FileName[0] == '.') continue;
}
if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_nFolders++;
if (!(dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
{
if (cb < FileNameLength)
{
__debugbreak();
}
else
{
memcpy(pszEnd, FileName, FileNameLength * sizeof(WCHAR));
Enum(pszEnd + FileNameLength, nLevel + 1, prefix - 1);
}
}
}
else if (nLevel >= _MaskCount || (!_szMask && nLevel == _MaskCount - 1))
{
_nFiles++;
DbgPrint("%s%u%u <%.*S>\n", prefix, nFileSizeLow, nFileSizeHigh, FileNameLength, FileName);
}
} while (FindNextFile(hFindFile, this));
if ((dwError = GetLastError()) == ERROR_NO_MORE_FILES)
{
dwError = NOERROR;
}
FindClose(hFindFile);
}
else
{
dwError = GetLastError();
}
if (dwError && dwError != ERROR_FILE_NOT_FOUND)
{
DbgPrint("%s[<%.*S>] err = %u\n", prefix, pszEnd - lpFileName, lpFileName, dwError);
}
}
void Test(PCWSTR pcszRoot)
{
char prefix[MAXUCHAR + 1];
memset(prefix, '\t', RTL_NUMBER_OF(prefix) - 1);
prefix[RTL_NUMBER_OF(prefix) - 1] = 0;
ENUM_CONTEXT ectx;
static PCWSTR Masks[] = { L"Program*", L"*", L"*.txt" };
static PCWSTR Masks2[] = { L"Program*", L"*" };
static PCWSTR Masks3[] = { L"Program Files*", L"Internet Explorer" };
// search Program*\*\*.txt with fixed deep level
ectx.StartEnum(pcszRoot, Masks, RTL_NUMBER_OF(Masks), 0, RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
// search *.txt files from Program*\*\ - any deep level
ectx.StartEnum(pcszRoot, Masks2, RTL_NUMBER_OF(Masks2), L"*.txt", RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
// search all files (*) from Program Files*\Internet Explorer\
ectx.StartEnum(pcszRoot, Masks3, RTL_NUMBER_OF(Masks3), L"*", RTL_NUMBER_OF(prefix) - 1, prefix + RTL_NUMBER_OF(prefix) - 1);
}

Win32 API Copy pictures files from a directory to new/existing directory

I want to copy all pictures file (like .jpeg, .bmp .gif) from "C:\" to a new/existing directory.
How can i write this? Something like (*.jpg|.bmp|.gif ) ???
Here is my code:
HANDLE hFile;
WIN32_FIND_DATA FindFileData;
char *strTheNameOfTheFile=new char[MAX_PATH];
char folder[100]="D:\\myPictures\\";
char tempFolder[100]="D:\\myPictures\\";
SetCurrentDirectory("D:\\");
hFile=FindFirstFile("*.jpg",&FindFileData);// Here is my problem | *.jpeg | *.bmp | *.gif
strTheNameOfTheFile = FindFileData.cFileName;
strcat(folder,strTheNameOfTheFile);
CopyFileEx(strTheNameOfTheFile,folder,NULL,NULL,NULL,NULL);
MessageBox(hDlg,strTheNameOfTheFile,"Oh",MB_OK);
strTheNameOfTheFile=NULL;
folder[0]='\0';
strcpy(folder,tempFolder);
while(FindNextFile(hFile,&FindFileData))
{
strTheNameOfTheFile = FindFileData.cFileName;
strcat(folder,strTheNameOfTheFile);
CopyFileEx(strTheNameOfTheFile,folder,NULL,NULL,NULL,NULL);
strTheNameOfTheFile=NULL;
folder[0]='\0';
strcpy(folder,tempFolder);
}
MessageBox(hDlg,"Done!","Finished",MB_OK);
FindClose(hFile);
Option 1: Put your code into a function (e.g., CopyMyFiles) that takes the extension as a parameter, and then you call that function once for each extension.
(Your code is a bit buggy, so you'll have to fix that up first.)
void CopyMyFiles(const char *pattern, const char *from, const char *to) {
const auto wild = std::string(from) + pattern;
WIN32_FIND_DATAA find_data;
HANDLE hfind = ::FindFirstFileA(wild.c_str(), &find_data);
if (hfind != INVALID_HANDLE_VALUE) {
do {
const auto source = std::string(from) + find_data.cFileName;
const auto target = std::string(to) + find_data.cFileName;
::CopyFileExA(source.c_str(), target.c_str(),
nullptr, nullptr, nullptr, 0);
} while (::FindNextFileA(hfind, &find_data));
::FindClose(hfind);
}
}
Then you can call this once for each pattern (extension).
const char *patterns[] = {"*.jpg", "*.gif", "*.bmp" };
const char *from = "D:\\";
const char *to = "D:\\pictures\\";
for (const auto &pattern : patterns)
CopyMyFiles(pattern, from, to);
Option 2: Enumerate all the files (using that pattern "*") and copy just the ones that match one of your extensions.

Copy files out of different folders to a specific on in C++

My problem is that i have several folders with the names "MORE0001" "MORE0002" etc and they contain one .SPE-file each.
I want to know if there is a way to extract all the .SPE-files to ONE folder by iterating through all the single-MORE...-folders.
I need sth. like this:
for (int i=0; i<10;i++){
newfile = getfile("directory/MORE%04d/filename.SPE", i);
// copy newfile to a new directory..
}
I hope you guys can help me find an easy solution, because i didn´t find a similar problem yet.
it´s just TOO easy..
i can just use the rename-function..
so it would be like:
rename(path/filename.SPE, newpath/filename.SPE);
thanks, but solved it myself ;)!
I have created one sample program, which might helps to resolve your problem.
#include<Windows.h>
#include<regex>
using namespace std;
void main()
{
regex e1("MORE\\d+");
string szDir = "C:\\*";
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFileA(szDir.c_str(), &ffd);
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (regex_match(ffd.cFileName,e1 ))
{
string s1 = ffd.cFileName;
string s2 = "C:\\" + s1 + "\\*";
WIN32_FIND_DATA ffdMORE;
HANDLE hFindMORE = FindFirstFile(s2.c_str(), &ffdMORE);
do
{
regex e2("\\w+.SPE");
if (regex_match(ffdMORE.cFileName,e2))
{
string commondir = "C:\\CommonDir\\";
string sourcePath = "C:\\" + s1 + "\\";
CopyFile(sourcePath.append(ffdMORE.cFileName).c_str(), commondir.append(ffdMORE.cFileName).c_str(), FALSE);
}
} while (FindNextFile(hFindMORE, & ffdMORE) != 0);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
}
Thanks,
Bharathraj

count the number of directories in a folder C++ windows

I have written a Java program that at one point counts the number of folders in a directory. I would like to translate this program into C++ (I'm trying to learn it). I've been able to translate most of the program, but I haven't been able to find a way to count the subdirectories of a directory. How would I accomplish this?
Thanks in advance
Here is an implementation using the Win32 API.
SubdirCount takes a directory path string argument and it returns a count of its immediate child subdirectories (as an int). Hidden subdirectories are included, but any named "." or ".." are not counted.
FindFirstFile is a TCHAR-taking alias which ends up as either FindFirstFileA or FindFirstFileW. In order to keep strings TCHAR, without assuming availabilty of CString, the example here includes some awkward code just for appending "/*" to the function's argument.
#include <tchar.h>
#include <windows.h>
int SubdirCount(const TCHAR* parent_path) {
// The hideous string manipulation code below
// prepares a TCHAR wildcard string (sub_wild)
// matching any subdirectory immediately under
// parent_path by appending "\*"
size_t len = _tcslen(parent_path);
const size_t alloc_len = len + 3;
TCHAR* sub_wild = new TCHAR[alloc_len];
_tcscpy_s(sub_wild, alloc_len, parent_path);
if(len && sub_wild[len-1] != _T('\\')) { sub_wild[len++] = _T('\\'); }
sub_wild[len++] = _T('*');
sub_wild[len++] = _T('\0');
// File enumeration starts here
WIN32_FIND_DATA fd;
HANDLE hfind;
int count = 0;
if(INVALID_HANDLE_VALUE != (hfind = FindFirstFile(sub_wild, &fd))) {
do {
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// is_alias_dir is true if directory name is "." or ".."
const bool is_alias_dir = fd.cFileName[0] == _T('.') &&
(!fd.cFileName[1] || (fd.cFileName[1] == _T('.') &&
!fd.cFileName[2]));
count += !is_alias_dir;
}
} while(FindNextFile(hfind, &fd));
FindClose(hfind);
}
delete [] sub_wild;
return count;
}