简体   繁体   English

如何检查特定文件夹中是否有文件?

[英]How to check if any file exist in specific folder?

I am using CreateProcess to copy files. 我正在使用CreateProcess复制文件。 Also I can catch different errors, if PC is offline, if directory does not exist. 如果PC处于离线状态,如果目录不存在,我也可以捕获其他错误。 Here is the problem I have: It returns 0 as error code, if all copying is successful and also returns 0 if there were zero files in source folder, so no copying is done. 这是我遇到的问题:如果所有复制成功,则返回0作为错误代码,如果源文件夹中的文件为零,则返回0,因此不进行复制。 I must detect whether there are no files in source folder. 我必须检测源文件夹中是否没有文件。 How can I do it in MFC VC++ 2013? 如何在MFC VC ++ 2013中做到这一点?

I have spent hours trying different solutions, but my knowledge is not high enough to implement all I find on internet. 我花了几个小时尝试不同的解决方案,但是我的知识不足以实现我在Internet上找到的所有内容。 So I have to ask for code, then I will understand. 所以我必须要代码,然后我才能理解。 Thank you in advance. 先感谢您。

This is code I use: 这是我使用的代码:

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);
}

If you can use directory_iterator from <filesystem> header file introduced in C++17: 如果可以使用C ++ 17中引入的<filesystem>头文件中的directory_iterator

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

May be needed std::experimental::filesystem instead of std::filesystem . 可能需要std::experimental::filesystem而不是std::filesystem

I have tried to port it to VC 2013, but only char version seems to compile 我尝试将其移植到VC 2013,但似乎只有char版本可以编译

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

If you want (or have) to use WinAPI: 如果您想要(或已经)使用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;
}

You can use the WIN32 function GetFileAttributes(..) to check if a file exists or not: 您可以使用WIN32函数GetFileAttributes(..)来检查文件是否存在:

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

Another way just might be trying to open the file (and if successful to close it again). 另一种方法可能只是尝试打开文件(如果成功则再次关闭它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查从键盘输入的特定 integer 值是否存在于 C++ 文件的一行或多行中 - How to check if a specific integer value input from keyboard exist in a line or more lines of a file in C++ 如何检查文件夹或文件是否被隐藏= - How to check whether a folder or a file is hidden= 如何使用C ++检查文件是否包含在文件夹中? - How to check a file is contained in a folder with C++? 如何检查zip文件或压缩文件是否存在? - How to check whether a zip file or compressed file is exist? 有什么方法可以检查文件名是否存在包含正则表达式的文件 - Is there any way to check whether a file exist with a file name contains regular expression 检查 StorageFolder 指向的文件夹是否仍然存在 - Check if folder pointed by StorageFolder still exist 如何检查数据库是否存在? - How to check database is exist or not? 如何检查C ++中是否存在记录(数据文件处理)? - How to check if a record exist in c++(data file handling)? 检查给定的多个文件是否存在 - Check if a given multiple file exist c ++中是否有任何功能可以检查在特定路径中给定的文件是否为脚本(.sh)文件 - is there any function in c++ to check whether a file given in a specific path is a script(.sh) file or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM