简体   繁体   中英

How to check if any file exist in specific folder?

I am using CreateProcess to copy files. Also I can catch different errors, if PC is offline, if directory does not exist. 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. I must detect whether there are no files in source folder. How can I do it in MFC VC++ 2013?

I have spent hours trying different solutions, but my knowledge is not high enough to implement all I find on 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:

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 .

I have tried to port it to VC 2013, but only char version seems to compile

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:

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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM