Smij01
Smij01

Reputation: 45

grep: input file ">":System cannot find the file specified

when I tried to execute a grep command in c++, I got the following error: grep: Input file: ">":System cannot find the file specified

Can anyone help me to resolve this?

         wchar_t* grepArg= L"\"D:\\grep\" -f \"C:\\Users\\ic014733\\AppData\\Local\\Temp\\patterns.tmp\" \"D:\\LOG2014_10_05.LOG\" >\"D:\\share\\result.txt\"";

        if ( !CreateProcessW(NULL, grepArg, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) )
        {
            DWORD errorMessageID = ::GetLastError();

        }
        else 
        {
            DWORD res = WaitForSingleObject(pi.hProcess, INFINITE);
            TerminateProcess(pi.hProcess, 0);                   
            PostThreadMessage(pi.dwThreadId, WM_QUIT, 0, 0) ;

            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);

        }

Upvotes: 0

Views: 788

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148890

You gave the answer in your comment : Error is like, grep considers '>' too as input file. Of course it does! Why should it not?

Let me explain a little: When you write grep -f strings_file file > result at a shell prompt (or cmd prompt under Windows), the shell parses the input line, indentifies the > redirection and processes the redirection:

  • open result file
  • start program grep with arguments (or command line for Windows) -f strings_file file and standard output redirected to result

But you are just starting the grep program with all the arguments in its command line, so no redirection can happen.

So what can you do? As none of your path contains spaces, you could just make cmd.exe do the redirection for you :

wchar_t* grepArg= L"cmd.exe /C \"D:\\grep -f C:\\Users\\ic014733\\AppData\\Local\\Temp\\patterns.tmp D:\\LOG2014_10_05.LOG > D:\\share\\result.txt";

That means that you start a new cmd.exe program with two parameters: /c saying execute that and your original command enclosed in quotes. But I never found the way to cleanly include quotes in quotes in Windows. That's the reason why I removed all the inner quotes from your command. It was possible because there was no space in any path.

The alternative would be to do the redirection by hand: open the output file, pass its handle in the hStdOutput member of si and declare it by setting STARTF_USESTDHANDLES in dwFlags member of same struct:

wchar_t* grepArg= L"\"D:\\grep\" -f \"C:\\Users\\ic014733\\AppData\\Local\\Temp\\patterns.tmp\" \"D:\\LOG2014_10_05.LOG\"";

HANDLE hstdout = CreateFile("D:\\share\\result.txt", "GENERIC_WRITE", 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// control hstdout is not null...
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdOutput = hstdout;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); // stdinput and stderr unchanged
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

if ( !CreateProcessW(NULL, grepArg, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) ) {
    ...

Upvotes: 1

Related Questions