0

I need to pass a parameter with space while executing below batch script using createprocess. Tried to give " for parameter alone but not working.

std::wstring setupDir =  L"\"D:\\Non Work\\New Bat File";
    std::wstring ExecutablePath =  L"\\Test.bat" ;
    std::wstring toolsSetupPath = L"";
    toolsSetupPath = L"CMD.exe /c " + setupDir +ExecutablePath +L"\" >C:\\AA 2.txt";
     DWORD commandLineLength = (DWORD)toolsSetupPath.size() + 1;
    LPWSTR process = (LPWSTR)_alloca(commandLineLength*sizeof(WCHAR));
    wcscpy_s(process,
             commandLineLength,
            toolsSetupPath.c_str());

    if (!CreateProcess(NULL, process, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, env,NULL, &si, &pi))
    {
        std::cout << "FAILURE" << std::endl;
        std::cout << GetLastError();
        system("pause");
        abort();
    }
Alex Mathew
  • 340
  • 1
  • 3
  • 12
  • 1
    I don't think it's related to the space in the file name, you can't redirect stdout to a file with the `createProcess` argument string – Alan Birtles Aug 13 '19 at 06:42
  • 1
    try this: `+ ExecutablePath + L" >\"C:\\AA2.txt\"";` Otherwise, try inverting the path backward slashes to a forward ones. Need to know the cmd actually. Also use `wstringstream` to simply formulating the commandline. – seccpur Aug 13 '19 at 06:56

1 Answers1

1

The problem here is you have space in the argument and for this kind of argument should be enclosed by quotes. So following particular line should solve the issue:

toolsSetupPath = L"CMD.exe /c " + setupDir + ExecutablePath + L"\" >\"C:\\AA 2.txt\"";
Santosh Dhanawade
  • 1,756
  • 14
  • 29
  • thanks @santosh I have a doubt. I created a bat which takes a parameter so calling that bat file with redirection eg: xx.bat &rty > "C:\test.txt" if I give & in parameter it's not writing to file else it's working fine. – Alex Mathew Aug 15 '19 at 14:27