Nisgrace 2017-08-14 08:03 采纳率: 100%
浏览 1142
已采纳

为什么代码只能搜索C盘中的文件,能不能改为全盘搜索?

#include
#include
using namespace std;

#ifdef UNICODE
#undef UNICODE
#endif

#include

BOOL MyFindFile(LPCSTR sFindPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sPath[MAX_PATH];
char sFormatFileName[MAX_PATH+2] = "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;

lstrcpy(sFormatFileName, sFindPath);///该函数复制一个字符串到缓冲区
lstrcat(sFormatFileName, "//*");///该函数将一个字符串附加在另一个字符串后面
lstrcat(sFormatFileName, sFindFileName);
lstrcat(sFormatFileName, "*");

hFind = FindFirstFile(sFormatFileName, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
    return FALSE;
}
else
{
    while (!fFinished)
    {
        lstrcpy(sPath, sFindPath);
        lstrcat(sPath, "//");
        lstrcat(sPath, FindFileData.cFileName);

        if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
        {
            if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
                cout << "  Folder " << ++uCountFolder << ". - " << sPath <<""<< endl;
        }
        else
            cout << "  File   " << ++uCountFile << ". - " << sPath <<""<< endl;

        if (!FindNextFile(hFind, &FindFileData))
        {
            if (GetLastError() == ERROR_NO_MORE_FILES)
            {
                fFinished = TRUE;
            }
            else
            {
                break;
            }
        }
    }

    FindClose(hFind);
}

return TRUE;

}

BOOL MyFindFolder(LPCSTR sPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sTemp[MAX_PATH];
char sFormatFileName[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;

MyFindFile(sPath, sFindFileName, uCountFolder, uCountFile);

lstrcpy(sFormatFileName, sPath);
lstrcat(sFormatFileName, "//*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
    return FALSE;
}
else
{
    while (!fFinished)
    {
        if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
        {
            if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
            {
                lstrcpy(sTemp, sPath);
                lstrcat(sTemp, "//");
                lstrcat(sTemp, FindFileData.cFileName);
                MyFindFolder(sTemp, sFindFileName, uCountFolder, uCountFile);
            }
        }

        if (!FindNextFile(hFind, &FindFileData))
        {
            if (GetLastError() == ERROR_NO_MORE_FILES)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }

    FindClose(hFind);
}

return TRUE;

}

void GetFileName(LPCSTR sFullPath, LPSTR sFilePath, LPSTR sFileName)
{
LPSTR p = (LPSTR)(sFullPath + lstrlen(sFullPath) - 1);
bool Flag(false);

while (p != sFullPath)
{
    if ('//' == *p || '/' == *p)
    {
        Flag = true;
        break;
    }
    p--;
}

if (Flag)
{
    lstrcpy(sFileName, p + 1);
    lstrcpy(sFilePath, sFullPath);
    sFilePath[p-sFullPath] = '/0';
}
else
{
    lstrcpy(sFileName, sFullPath);
    GetFullPathName(".", MAX_PATH, sFilePath, NULL);///从一指定文件得到文件的路径
}

}

ULONGLONG MyStartFind(LPCSTR sFindFileName)
{
char sPath[MAX_PATH];
char sFileName[MAX_PATH];
ULONGLONG uCountFolder(0);
ULONGLONG uCountFile(0);

GetFileName(sFindFileName, sPath, sFileName);
MyFindFolder(sPath, sFileName, uCountFolder, uCountFile);

if (uCountFolder + uCountFile)
{
    cout << "------------------------------------------------------------"<< endl;
    cout << "Total Folders:"<< uCountFolder << "" << endl;
    cout << "Total Files:  "<< uCountFile << "" << endl;
}
else
    cout << "Couldn't Find File." << endl;

return uCountFolder + uCountFile;

}

int main(int argc, char *argv[])
{
char sFindFileName[MAX_PATH];

if (argc < 2)
{
    cout << "Enter Find File Name:";
    cin >> sFindFileName;
}
else
{
    lstrcpy(sFindFileName, argv[1]);
}
MyStartFind(sFindFileName);

#ifdef _DEBUG
system("pause");
#else
if (argc < 2)
system("pause");
#endif

return (0);

}

  • 写回答

2条回答 默认 最新

  • Nisgrace 2017-08-15 07:00
    关注

    。。代码放到相应的盘下就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序