代码之家  ›  专栏  ›  技术社区  ›  Orion310591

如何检查特定文件夹中是否存在任何文件?

  •  0
  • Orion310591  · 技术社区  · 8 年前

    我正在使用 CreateProcess 复制文件。此外,我可以捕捉不同的错误,如果PC离线,如果目录不存在。 这是我遇到的问题:如果所有复制都成功,则返回0作为错误代码;如果源文件夹中没有文件,则返回0,因此不进行复制。我必须检测源文件夹中是否没有文件。如何在MFC VC++2013中实现?

    我花了几个小时尝试不同的解决方案,但我的知识还不足以实现我在互联网上找到的所有解决方案。所以我必须要代码,然后我才能理解。提前谢谢你。

    这是我使用的代码:

    temp_dest = _T("/min /c xcopy \"D:\\Test\\*.*\" \"") + m_destination + _T("\" /Y /E /Q");
    LPTSTR temp_dest2 = (LPTSTR)(LPCTSTR)temp_dest;
    STARTUPINFO            sinfo;
    PROCESS_INFORMATION    pinfo;
    memset(&sinfo, 0, sizeof(STARTUPINFO));
    memset(&pinfo, 0, sizeof(PROCESS_INFORMATION));
    sinfo.dwFlags = STARTF_USESHOWWINDOW;
    sinfo.wShowWindow = SW_HIDE;
    BOOL bSucess = CreateProcess(L"C:\\Windows\\System32\\cmd.exe", temp_dest2, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &sinfo, &pinfo);
    DWORD dwCode;
    TerminateProcess(pinfo.hProcess, 2);
    GetExitCodeProcess(pinfo.hProcess, &dwCode);
    TCHAR msg2[100];
    StringCbPrintf(msg2, 100, TEXT("%X"), dwCode); 
    MessageBox(msg2, (LPCWSTR)L"DWCode 2", MB_OK | MB_ICONERROR);
    if (dwCode == 4)
    {
        MessageBox((LPCWSTR)L"DW 4", (LPCWSTR)L"Path not found", MB_OK | MB_ICONERROR);
    }
    if (dwCode == 2)
    {
        MessageBox((LPCWSTR)L"DW 4", (LPCWSTR)L"PC Offline", MB_OK | MB_ICONERROR);
    }
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   Lukas Thomsen    8 年前

    你可以使用win32函数 GetFileAttributes(..) 要检查文件是否存在,请执行以下操作:

    if (GetFileAttributes("C:\\test.txt") != INVALID_FILE_ATTRIBUTES)
    {
        /* C:\test.txt is existing */
    }
    

    另一种方法可能是尝试打开文件(如果成功再次关闭它)。

        2
  •  4
  •   Daniel Sęk    8 年前

    如果你能使用 directory_iterator <filesystem> 在C++ 17中引入的头文件:

    bool IsEmptyDirectory( const wchar_t* dir )
    {
        return std::filesystem::directory_iterator( std::filesystem::path( dir ) )
                == std::filesystem::directory_iterator();
    }
    

    可能需要 std::experimental::filesystem 而不是 std::filesystem .

    我试着把它移植到VC 2013,但是 char 版本似乎已编译

    bool IsEmptyDirectory( const char* dir )
    {
        return std::tr2::sys::directory_iterator( std::tr2::sys::path( dir ) )
                == std::tr2::sys::directory_iterator();
    }
    

    如果要(或必须)使用winapi:

    bool IsEmptyDirectory( const wchar_t* dir )
    {
        wstring mask( dir);
        mask += L"\\*";
    
        WIN32_FIND_DATA data;
        HANDLE  find_handle = FindFirstFile( mask.c_str(), &data );
        if ( find_handle == INVALID_HANDLE_VALUE )
        {
            // Probably there is no directory with given path.
            // Pretend that it is empty.
            return true;
        }
    
        bool empty = true;
        do
        {
            // Any entry but . and .. means non empty folder.
            if ( wcscmp( data.cFileName, L"." ) != 0 && wcscmp( data.cFileName, L".." ) != 0 )
                empty = false;
        } while ( empty && FindNextFile( find_handle, &data ) );
    
        FindClose( find_handle );
    
        return empty;
    }