#include "StdAfx.h" #include "filehelp.h" #include namespace filehelping { filehelpImpl::filehelpImpl(void) { } filehelpImpl::~filehelpImpl(void) { } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getall(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN OUT STR_VEC *pvtfiles, IN OUT STR_VEC *pvtnames, IN OUT STR_VEC *pvtnames_noext, IN OUT STR_VEC *pvtfolders) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (!pvtfiles || !pvtnames || !pvtfolders || !pvtnames_noext) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames = pvtnames; m_pvtfolders = pvtfolders; m_pvtnames_noext = pvtnames_noext; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findall(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getsubfolder(IN LPCTSTR lpfolder, IN STR_VEC *pvtfolders) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtfolders == NULL) return FALSE; m_pvtfolders = pvtfolders; findsubfolder(lpfolder); return TRUE; } BOOL filehelpImpl::getallsubfolder(IN LPCTSTR lpfolder, IN STR_VEC *pvtfolders) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtfolders == NULL) return FALSE; m_pvtfolders = pvtfolders; findallsubfolder(lpfolder); return TRUE; } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getfiles_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtfiles == NULL) return FALSE; m_pvtfiles = pvtfiles; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfiles_findin_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfiles_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtfiles == NULL) return FALSE; m_pvtfiles = pvtfiles; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfiles_findout_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfiles_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtfiles == NULL) return FALSE; m_pvtfiles = pvtfiles; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfiles_within_subfolder(lpfolder, lpfindext); return TRUE; } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getnames_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_findin_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getnames_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_findout_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getnames_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_within_subfolder(lpfolder, lpfindext); return TRUE; } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getnames_findin_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_findin_subfolder_ex(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getnames_findout_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_findout_subfolder_ex(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getnames_within_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames == NULL) return FALSE; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findnames_within_subfolder_ex(lpfolder, lpfindext); return TRUE; } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getfilesnames_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_findin_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfilesnames_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_findout_subfolder(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfilesnames_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_within_subfolder(lpfolder, lpfindext); return TRUE; } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::getfilesnames_findin_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_findin_subfolder_ex(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfilesnames_findout_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_findout_subfolder_ex(lpfolder, lpfindext); return TRUE; } BOOL filehelpImpl::getfilesnames_within_subfolder_ex(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames) { // 路径不存在; if (!PathFileExists(lpfolder)) return FALSE; // 指针空; if (pvtnames || !pvtnames) return FALSE; m_pvtfiles = pvtfiles; m_pvtnames_noext = pvtnames; // 判断扩展名有效性; if (!iscorrectext(lpfindext)) return FALSE; findfilesnames_within_subfolder_ex(lpfolder, lpfindext); return TRUE; } #if 1 ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::comparename_2file(IN LPCTSTR file1, IN LPCTSTR file2) { if (!file1 || !file2) return FALSE; if (!PathFileExists(file1) || !PathFileExists(file2)) return FALSE; TString strfile1 = file1; TString strfile2 = file2; strfile1 = getfilename(strfile1); strfile2 = getfilename(strfile2); return !strfile1.compare(strfile2); } BOOL filehelpImpl::comparename_findin_names(IN LPCTSTR name) { if (!name || m_pvtnames_noext) return FALSE; TString strname = name; uppercase(strname); for (STR_VEC::iterator it = m_pvtnames_noext->begin(); it != m_pvtnames_noext->end(); it++) { uppercase(*it); if (it->compare(strname) == 0) { // 打印日志,或提示用户同名文件路径在哪; return TRUE; } } return FALSE; } BOOL filehelpImpl::comparename_findin_names(IN LPCTSTR name, IN STR_VEC &vtnames) { if (!name) return FALSE; TString strname = name; uppercase(strname); for (STR_VEC::iterator it = vtnames.begin(); it != vtnames.end(); it++) { uppercase(*it); if (it->compare(strname) == 0) { // 打印日志,或提示用户同名文件路径在哪; return TRUE; } } return FALSE; } BOOL filehelpImpl::comparename_findin_files(IN LPCTSTR name, IN STR_VEC &vtfiles) { if (!name) return FALSE; int nIndex = 0; TString strfile; TString strname = name; uppercase(strname); for (STR_VEC::iterator it = vtfiles.begin(); it != vtfiles.end(); it++) { if ((nIndex = it->find_last_of(_T('\\'))) != TString::npos) { strfile = it->substr(nIndex); uppercase(strfile); if (strfile.compare(strname) == 0) { // 打印日志,或提示用户同名文件路径在哪; return TRUE; } } } return FALSE; } BOOL filehelpImpl::comparename_findin_folder(IN LPCTSTR name, IN LPCTSTR folder, IN LPCTSTR findext, IN BOOL bsubfolder) { if (!name || !iscorrectext(findext)) return FALSE; STR_VEC vtnames; if (bsubfolder) { if (!getnames_findin_subfolder_ex(folder, findext, &vtnames)) return FALSE; } else { if (!getnames_findout_subfolder_ex(folder, findext, &vtnames)) return FALSE; } return comparename_findin_names(name, vtnames); } INT filehelpImpl::comparenames_findin_names(IN STR_VEC vtSrcnames, IN STR_VEC vtDesnames, OUT STR_VEC *pvtIsonym /* = NULL */) { if (vtSrcnames.size() == 0 || vtDesnames.size() == 0) return 0; INT nCount = 0; for (STR_VEC::iterator it1 = vtSrcnames.begin(); it1 != vtSrcnames.end(); it1++) { for (STR_VEC::iterator it2 = vtDesnames.begin(); it2 != vtDesnames.end(); it2++) { uppercase(*it1); uppercase(*it2); if (it1->compare(*it2) == 0) { nCount++; if (pvtIsonym) pvtIsonym->push_back(*it1); break; } } } return nCount; } INT filehelpImpl::comparenames_findin_files(IN STR_VEC vtnames, IN STR_VEC vtfiles, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { if (vtnames.size() == 0 || vtfiles.size() == 0) return 0; if (!iscorrectext(findext)) return 0; INT nCount = 0; for (STR_VEC::iterator it = vtnames.begin(); it != vtnames.end(); it++) { if (comparename_findin_files(it->c_str(), vtfiles)) { if (pvtIsonym) pvtIsonym->push_back(*it); nCount++; } } return nCount; } INT filehelpImpl::comparenames_findin_folder(IN STR_VEC vtnames, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { if (vtnames.size() == 0 || !PathFileExists(folder)) return 0; if (!iscorrectext(findext)) return 0; STR_VEC vtnames2; if (bsubfolder) { getnames_findin_subfolder(folder, findext, &vtnames2); } else { getnames_findout_subfolder(folder, findext, &vtnames2); } return comparenames_findin_names(vtnames, vtnames2, pvtIsonym); } ////////////////////////////////////////////////////////////////////////// BOOL filehelpImpl::comparefile_findin_names(IN LPCTSTR file, IN STR_VEC& vtnames) { if (vtnames.size() == 0) return FALSE; if (!file || !PathFileExists(file)) return FALSE; TString strfile = file; TString name = getfilename(strfile); return comparename_findin_names(name.c_str(), vtnames); } BOOL filehelpImpl::comparefile_findin_files(IN LPCTSTR file, IN STR_VEC& vtfiles, IN LPCTSTR findext) { if (!file || !PathFileExists(file)) return FALSE; if (!iscorrectext(findext)) return FALSE; TString strfile = file; TString name = getfilename(strfile); // 过滤掉不需要的扩展名; keepdownext(findext, vtfiles); return comparename_findin_files(name.c_str(), vtfiles); } BOOL filehelpImpl::comparefile_findin_folder(IN LPCTSTR file, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext) { if (!file || !PathFileExists(file) || !folder || !PathFileExists(folder)) return FALSE; if (!iscorrectext(findext)) return FALSE; TString strfile = file; TString name = getfilename(strfile); STR_VEC vtnames; if (bsubfolder) { getnames_findin_subfolder_ex(folder, findext, &vtnames); } else { getnames_findout_subfolder_ex(folder, findext, &vtnames); } return comparename_findin_names(name.c_str(), vtnames); } INT filehelpImpl::comparefiles_findin_files(IN STR_VEC &vtfiles1, IN STR_VEC &vtfiles2, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { if (vtfiles1.size() == 0 || vtfiles2.size() == 0) return 0; if (!iscorrectext(findext)) return 0; // 过滤掉不需要的扩展名; keepdownext(findext, vtfiles1); // 过滤掉不需要的扩展名; keepdownext(findext, vtfiles2); INT nCount = 0; TString name; for (STR_VEC::iterator it = vtfiles1.begin(); it != vtfiles1.end(); it++) { name = getfilename(*it); if (comparename_findin_files(name.c_str(), vtfiles2)) { if (pvtIsonym) pvtIsonym->push_back(name); nCount++; } } return nCount; } INT filehelpImpl::comparefiles_findin_folder(IN STR_VEC &vtfiles, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { if (vtfiles.size() == 0 || !folder || !PathFileExists(folder)) return 0; if (!iscorrectext(findext)) return 0; STR_VEC vtnames; if (bsubfolder) { getnames_findin_subfolder_ex(folder, findext, &vtnames); } else { getnames_findout_subfolder_ex(folder, findext, &vtnames); } return comparenames_findin_files(vtnames, vtfiles, findext, pvtIsonym); } INT filehelpImpl::comparefolderself(IN LPCTSTR folder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { OutputDebugString(_T("\n")); if (!folder || !PathFileExists(folder) || !iscorrectext(findext)) return 0; STR_VEC vtsubfolders; vtsubfolders.push_back(folder); // 包含当前目录; getsubfolder(folder, &vtsubfolders); int n = vtsubfolders.size(); STR_VEC *pvtfiles = new STR_VEC[n]; for (STR_VEC::iterator it = vtsubfolders.begin(); it != vtsubfolders.end(); it++) { getfiles_findin_subfolder(it->c_str(), findext, &pvtfiles[--n]); } STR_VEC vtnames; n = vtsubfolders.size(); TString name; for (int i = 0; i < n; i++) { for (STR_VEC::iterator it = pvtfiles[i].begin(); it != pvtfiles[i].end(); it++) { name = getfilename(*it); if (comparename_findin_names(name.c_str(), vtnames)) { // 打印日志,提示客户重名的相片; OutputDebugString(it->c_str()); OutputDebugString(_T("\n")); pvtIsonym->push_back(it->c_str()); } else { vtnames.push_back(name); } } } if (pvtfiles) delete []pvtfiles; pvtfiles = NULL; return 0; } INT filehelpImpl::comparefolder_findin_folder(IN LPCTSTR folder1, IN LPCTSTR folder2, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */) { return 0; } #endif };