コード例 #1
0
ファイル: PathHelper.cpp プロジェクト: pyq881120/urltraveler
BOOL PathHelper::GetAllFile(
    const std::wstring & strDir,
    CStdStringVector &vecFile,
    const std::wstring &strDirMask,
    const std::wstring &strFileMask
)
{
    BOOL bResult = FALSE;
    vecFile.clear();

    CStdStringVector vecSubDir;

    ///获取所有子目录
    bResult = GetAllSubDir(strDir, vecSubDir, strDirMask);

    ///将目录自己也压入进去以查找文件
    vecSubDir.push_back(strDir);

    if (vecSubDir.size() > 0)
    {
        CStdStringVector::iterator iter = vecSubDir.begin();
        while (iter != vecSubDir.end())
        {
            CStdStringVector vecTmpFile;
            GetDirFile(*iter, vecTmpFile, strFileMask);

            vecFile.insert(vecFile.end(), vecTmpFile.begin(), vecTmpFile.end());

            iter ++;
        }
    }

    bResult = (vecFile.size() > 0) ;
    return bResult;
}
コード例 #2
0
ファイル: PathHelper.cpp プロジェクト: pyq881120/urltraveler
BOOL PathHelper::RemoveDirAllSubDir(const std::wstring &strPath)
{
    ///获取所有子目录
    CStdStringVector vecSubDir;
    BOOL bResult = GetAllSubDir(strPath, vecSubDir);

    ///删除所有的子目录,遍历存放目录的数组,删除每层目录,从最深的目录开始删除,进行逆向访问
    if (vecSubDir.size() > 0)
    {
        CStdStringVector::const_reverse_iterator iter = vecSubDir.rbegin();
        while (iter != vecSubDir.rend())
        {
            bResult = RemoveDirectoryW(iter->c_str());

            iter ++;
        }
    }
    else
    {
        bResult = true;
    }

    return bResult;
}
コード例 #3
0
ファイル: PathHelper.cpp プロジェクト: pyq881120/urltraveler
BOOL PathHelper::GetDirFile(
    const std::wstring &strDir,
    CStdStringVector &vecFile,
    const std::wstring &strFileMask
)
{
    BOOL bResult = FALSE;
    vecFile.clear();
    /*
    下面的代码为访问目录,和操作系统相关,目前为windows操作系统的相关代码
    */

    std::wstring strFileName;
    //存放查找的当前目录
    std::wstring strCurDirName;
    //存放查找中碰到的中间目录
    std::wstring strSubDirName;
    //用来模拟递归用的存储中间遇到的子目录
    std::queue<std::wstring> recursiveDirQueue;

    strCurDirName = strDir;
    strFileName = strDir;
    strFileName += L"\\";
    strFileName += strFileMask;

    HANDLE hFindFile;
    WIN32_FIND_DATAW fileinfo;
    hFindFile = FindFirstFileW(strFileName.c_str(), &fileinfo);

    while (INVALID_HANDLE_VALUE != hFindFile)
    {
        if ((fileinfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FALSE)
        {
            std::wstring strTmpFile = strCurDirName;
            strTmpFile += L"\\";
            strTmpFile += fileinfo.cFileName;
            //文件不为子目录
            vecFile.push_back(strTmpFile);
            /**
            push(fileinfo.cFileName);
            printf ("The  file found is %s\n", fileinfo.cFileName);
            */
        }
        else
        {
#if 0
            //将查找中间碰到的子目录全部存入dirQueue队列中,以后还要对该目录进行查找
            bResult = DirFilter(fileinfo.cFileName);
            if (true != bResult) //不为要过滤掉的.和..目录则压入目录队列中
            {
                strSubDirName += strCurDirName;
                strSubDirName += "\\";
                strSubDirName += fileinfo.cFileName;
                ///sprintf(strSubDirName,"%s\\%s" , strCurDirName, fileinfo.cFileName);
                recursiveDirQueue.push(strSubDirName);
                vecSubDir.push_back(strSubDirName);
                printf ("The  subdir found is %s\n", strSubDirName.c_str());
            }
#endif
        }

        bResult = FindNextFileW(hFindFile, &fileinfo);

        if (FALSE == bResult)
        {
            /*
            if (GetLastError() == ERROR_NO_MORE_FILES)
            {
            printf("No more %s files.", m_StrFileName);
            }
            else
            {
            printf("Couldn't find next file.");
            }
            */

            FindClose(hFindFile);
            break;
        }
    }

    bResult = (vecFile.size() > 0) ;
    return bResult;
}