123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- #include "StdAfx.h"
- #include "filehelp.h"
- #include <shlwapi.h>
- 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
- };
|