Hi guys
I got a problem with the code
I really have tried everything but in vain (I'm just a beginner in C++)
The task is to display all files in selected directory and all subdirectories as well.
I want to display all data in listview.(I have created it) But the code I found do it using _tprintf/(Looks very easy to remake the code,but I dont know how to change the parameters of function that using in Class constructor)...Give me an advice please.
Look in code(written by some other man):
Main function where he calls the FileEnumerator function:
#include "stdafx.h"
#include "FileEnumerator.h"
bool FileEnumeratorCallback(LPCTSTR lpszItemName, LPCTSTR lpszItemDir, LPWIN32_FIND_DATA lpItemFindData, LPVOID lpUserData)
{
if(lpItemFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
_tprintf(_T("[%s]\n"), CString(lpszItemDir) +lpszItemName);
else
_tprintf(_T("%s\n"), CString(lpszItemDir) + lpszItemName);
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
if(argc !=3)
exit(_ftprintf(stderr, _T("\nUsage: %s <search dir=""> <search pattern="">\n\nExample:\n %s c:\\ *.cpp\n"), argv[0],argv[0]));
CFileEnumerator _EnumObj(FileEnumeratorCallback, argv[1], argv[2]);
_EnumObj.RecurseSubDirectories() = true;
_EnumObj.ReportMatchingDirsAlso() = false;
_EnumObj.IgnoreDots() = true;
_tprintf(_T("\n\nFile Listing for %s with pattern %s\n\tRecursion:[%s]\n\tReportMatchingDirs:[%s]\n\n"),
argv[1],argv[2],_EnumObj.RecurseSubDirectories()?_T("ON"):_T("OFF"), _EnumObj.ReportMatchingDirsAlso()?_T("ON"):_T("OFF"));
if(!_EnumObj.EnumerateFiles())
{
_tprintf(_T("\nEnumeration was stopped by user !!\n"));
}
_tprintf(_T("\n\nAll directories under %s \n\n"),argv[1]);
if(!_EnumObj.ListAllDirectories())
{
_tprintf(_T("\nEnumeration was stopped by user !!\n"));
}
return 0;
}
And the code proper:
#ifndef __FILE_ENUMERATOR_H_EEF8FBA5_B0F1_4682_9614_2027C16002F4
#define __FILE_ENUMERATOR_H_EEF8FBA5_B0F1_4682_9614_2027C16002F4
#include "PathHelper.h"
typedef bool (*PFN_EnumeratorCallback)(LPCTSTR lpszItemName, LPCTSTR lpszItemDir, LPWIN32_FIND_DATA lpszItemFindData, LPVOID lpUserData);
class CFileEnumerator
{
CString m_strSearchDir;
CString m_strFileToSearch;
bool m_bRecurseSubDirectories;
bool m_bIgnoreDots;
bool m_bReportMatchingDirsAlso;
PFN_EnumeratorCallback m_pfnEnumCallback;
inline static bool EnumCallback(LPCTSTR, LPCTSTR, LPWIN32_FIND_DATA, LPVOID)
{
return false;
}
public:
CFileEnumerator(PFN_EnumeratorCallback pfnEnumCallback = CFileEnumerator::EnumCallback,
LPCTSTR lpszSearchDir = _T("."),
LPCTSTR lpszFileToSearch = _T("*.*"),
bool bRecurseSubDirectories = true,
bool bReportMatchingDirsAlso = true,
bool bIgnoreDots = true
)
{
m_pfnEnumCallback = pfnEnumCallback;
m_strSearchDir = lpszSearchDir;
m_strFileToSearch = lpszFileToSearch;
m_bRecurseSubDirectories = bRecurseSubDirectories;
m_bReportMatchingDirsAlso = bReportMatchingDirsAlso;
m_bIgnoreDots = bIgnoreDots;
}
~CFileEnumerator(void)
{
}
PFN_EnumeratorCallback& FileEnumeratorCallback()
{
return m_pfnEnumCallback;
}
bool& IgnoreDots() { return m_bIgnoreDots; }
bool& RecurseSubDirectories() { return m_bRecurseSubDirectories; }
CString& FileToSearch() { return m_strFileToSearch; }
LPCTSTR FileToSearch() const { return m_strFileToSearch; }
CString& SearchDir() { return m_strSearchDir; }
LPCTSTR SearchDir() const { return m_strSearchDir; }
bool& ReportMatchingDirsAlso() { return m_bReportMatchingDirsAlso; }
HANDLE FindFirstSubDir(LPCTSTR lpszDir, LPWIN32_FIND_DATA pFindData) const
{
HANDLE hFind = FindFirstFile(CString(lpszDir) + _T("*.*"), pFindData);
if(hFind == INVALID_HANDLE_VALUE) return hFind;
do
{
if(PathIsDots(pFindData->cFileName))
{
if(m_bIgnoreDots == false)
return hFind;
continue;
}
if(!(pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue;
return hFind;
}while(FindNextFile(hFind, pFindData));
return INVALID_HANDLE_VALUE;
}
BOOL FindNextSubDir(HANDLE hFind, LPWIN32_FIND_DATA pFindData) const
{
while(FindNextFile(hFind, pFindData))
{
if(PathIsDots(pFindData->cFileName))
{
if(m_bIgnoreDots == false)
return true;
continue;
}
if(!(pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue;
return true;
}
return false;
}
bool ListAllDirectories(LPVOID lpUserData = NULL) const
{
WIN32_FIND_DATA ffData;
CString strSearchDir((LPCTSTR)SearchDir());
PathAddBackSlash(strSearchDir);
HANDLE hFind = FindFirstSubDir(strSearchDir, &ffData);
if(hFind == INVALID_HANDLE_VALUE) return true;
bool bContinue = true;
do
{
if(false == (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData))
return false;
if(m_bRecurseSubDirectories)
{
CFileEnumerator feDir(m_pfnEnumCallback,
strSearchDir + ffData.cFileName,
_T("*.*"),
m_bRecurseSubDirectories,
m_bReportMatchingDirsAlso,
m_bIgnoreDots);
bContinue = feDir.ListAllDirectories(lpUserData);
}
}while(bContinue && FindNextSubDir(hFind, &ffData));
FindClose(hFind);
return true;
}
bool EnumerateFiles(LPVOID lpUserData = NULL) const
{
bool bContinue = true;
CString strSearchDir((LPCTSTR)SearchDir());
PathAddBackSlash(strSearchDir);
WIN32_FIND_DATA ffData;
HANDLE hFind = FindFirstFile(strSearchDir + (LPCTSTR)FileToSearch(), &ffData);
if(hFind == INVALID_HANDLE_VALUE)
{
if(m_bRecurseSubDirectories)
{
CFileEnumerator feDir(EnumCallback,
strSearchDir,
_T("*.*"),
m_bRecurseSubDirectories,
m_bReportMatchingDirsAlso,
true);
hFind = feDir.FindFirstSubDir(strSearchDir, &ffData);
if(hFind == INVALID_HANDLE_VALUE) return true;
do
{
bContinue = CFileEnumerator( m_pfnEnumCallback,
strSearchDir + ffData.cFileName,
FileToSearch(),
m_bRecurseSubDirectories,
m_bReportMatchingDirsAlso,
m_bIgnoreDots).EnumerateFiles(lpUserData);
}while(bContinue && feDir.FindNextSubDir(hFind, &ffData));
FindClose(hFind);
return bContinue;
}
else
return true;
}
do
{
if(PathIsDots(ffData.cFileName))
{
if(m_bIgnoreDots == false)
bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
continue;
}
if((ffData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if(m_bReportMatchingDirsAlso)
bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
}
else
{
bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
}
}while(bContinue && FindNextFile(hFind, &ffData));
FindClose(hFind);
if(bContinue && m_bRecurseSubDirectories)
{
CFileEnumerator feDir(EnumCallback,
strSearchDir,
_T("*.*"),
m_bRecurseSubDirectories,
m_bReportMatchingDirsAlso,
true);
hFind = feDir.FindFirstSubDir(strSearchDir, &ffData);
if(hFind == INVALID_HANDLE_VALUE) return true;
do
{
bContinue = CFileEnumerator( m_pfnEnumCallback,
strSearchDir + ffData.cFileName,
FileToSearch(),
m_bRecurseSubDirectories,
m_bReportMatchingDirsAlso,
m_bIgnoreDots).EnumerateFiles(lpUserData);
}while(bContinue && feDir.FindNextSubDir(hFind, &ffData));
FindClose(hFind);
}
return bContinue;
}
};
#endif
As yoy see I 'd like to change _tprintf() and Insert smth like that
VOID InsertIN_LIST(HWND hFileListView,LVITEM lv,int& a,LPCTSTR lpszItemName){
lv.iItem=a;
ListView_InsertItem(hFileListView, &lv);
ListView_SetItemText(hFileListView, a, 1, (LPWSTR)lpszItemName);
a++;
}
I've tried:
1)
bool FileEnumeratorCallback(LPCTSTR lpszItemName, LPCTSTR lpszItemDir, LPWIN32_FIND_DATA lpItemFindData,LPVOID lpUserData,HWND hFileListView,LVITEM lv){bla-bla}}
And changed:
CFileEnumerator(PFN_EnumeratorCallback pfnEnumCallback = CFileEnumerator::EnumCallback,
LPCTSTR lpszSearchDir = _T("."),
LPCTSTR lpszFileToSearch = _T("*.*"),
HWND hFileListView=NULL,
LVITEM lv=lv)
But always got an Error: "lv" and "hFileListView" as undeclared identifiers.