1

I have a function that returns content of folder:

void getDir(const char* d, vector<string> & f)
{
    FILE* pipe =  NULL;
    string pCmd = "dir /B /S " + string(d);
    char buf[256];
    if( NULL == (pipe = _popen(pCmd.c_str(),"rt")))
    {
        cout<<"[Expletive deleted]"<<endl;
        return;
    }
    while (!feof(pipe))
    {
        if(fgets(buf,256,pipe) != NULL)
        {
            f.push_back(string(buf));
        }
    }
    _pclose(pipe);
}

The problem is that string pCMD should be equal to "dir /B /S c:\Users\Tom", but after converting char to string it removes the backslashes like "dir /B /S c:UsersTom". I've tried calling the function in these ways:

getDir("c:\Users\Tom", files);
getDir("c:/Users/Tom", files);
getDir("c:\\Users\Tom", files);
getDir("c:\\Users\\Tom", files);

But every time I get a failure (vector files is empty...). Does anybody know what I'm doing wrong?

Greenonline
  • 1,330
  • 8
  • 23
  • 31
Meskis
  • 21
  • 2
  • 1
    Gonna assume this is on some windows box. I don't suppose you're "Tom" are you? Any chance "Tom" gave you read-access to his user directory? And you're running this from a *console* app, right? It won't work from a subsystem-Windows app (nor will _popen return NULL, which you're relying on). – WhozCraig Jan 07 '15 at 11:49
  • User tom is just an example, actually I need to get content of D:\Music folder and I have full access to that folder :D But you are right, I'm running this from console app. So do you have any suggestions how can I get folder content simply? – Meskis Jan 07 '15 at 12:20
  • I suppose the same error happens if you lose the `feof(pipe)` in the while-condition ([which is wrong anyway](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) and move the `(fgets(buf,256,pipe) != NULL)` to the while-condition where it likely belongs ? – WhozCraig Jan 07 '15 at 17:02

2 Answers2

2

Put double backslash, as single backslash stands for wildchar, thus to have backslash as a character, it should be preceded by another backslash

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

Thank you all for help, especially thanks for Debasish Jana, you were right. To get backslash as a char I preceded it by another backslash. But I found another way to get folder content using FindFirstFile() FindNextFile() and I'd like to share with it:

std::vector<std::string> getContent(TCHAR *dir, TCHAR *extention){

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    char ch[260];
    char defChar;
    memset(patter, 0x00, MAX_PATH);
    _stprintf_s(patter, extention, dir);
    hFind = FindFirstFile(patter, &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE) 
    {
        printf ("FindFirstFile failed (%d)\n", GetLastError());
        //return 1;
    } 
    else 
    {
        do
        {
            //ignore current and parent directories
            if(_tcscmp(FindFileData.cFileName, TEXT("."))==0 || _tcscmp(FindFileData.cFileName, TEXT(".."))==0)
                continue;

            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                //ignore directories
            }
            else
            {
                //list the Files
                files.resize(files.size()+1);
                WideCharToMultiByte(CP_ACP,0,FindFileData.cFileName,-1, ch,260,&defChar, NULL);
                files[files.size()-1] = std::string(ch);
                //_tprintf (TEXT("%s\n"), FindFileData.cFileName);
            }
        }
        while (FindNextFile(hFind, &FindFileData));
        FindClose(hFind);
    }
    return files;
}

Here is how to call this function:

vector<string> ss;
TCHAR  *directory = TEXT("D:\\Music");
TCHAR  *ext = TEXT("%s\\*.mp3");
ss  = getContent(TCHAR *dir, TCHAR *extention)
Meskis
  • 21
  • 2