Link to home
Start Free TrialLog in
Avatar of DaveB080597
DaveB080597

asked on

Sorting an external file from MFC app

I want to sort a file from an MFC app, where I have the full filename.
Would be happy to use cmd.exe external sort like
sort <myfile.txt >mysortedfile.txt
but can't see how to do this with shellexecute etc silently.

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Try with WinExec:

::WinExec("sort <myfile.txt >mysortedfile.txt", SW_HIDE);

Pay attention to file paths.

Good luck,
Jaime.
Avatar of DaveB080597
DaveB080597

ASKER

OK, I tried this but no sign of it happening.
I added
      ::WinExec("sort <c:\\myfile.txt >c:\\mysortedfile.txt", SW_HIDE);
to a little test app, but it didn't create the output file.
I changed SW_HIDE to SW_SHOW for testing and saw the cmd window flash by, but too quick to see what it said.
I also tried
     ::WinExec("echo aaaa >c:\\myechofile.txt", SW_HIDE);
but still couldn't create an output file.

I then looked up WinExec and it said to use CreateProcess and gave some sample code - looked useful because it also waited for sort.
But I tried this code with same result... plus couldn't see how to make it SW_HIDE...
Here's the routine I 'borrowed' from the MSDN doc
and I called it with the line
    ShellExecuteCommandWait("sort <c:\\myfile.txt >c:\\mysortedfile.txt");
Any ideas what I'm doing wrong here? thanks


static bool ShellExecuteCommandWait(LPCTSTR cmd) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // Start the child process.
    if (!CreateProcess(NULL, // No module name (use command line).
        (LPSTR)cmd,            // Command line.
        NULL,                  // Process handle not inheritable.
        NULL,                  // Thread handle not inheritable.
        FALSE,                  // Set handle inheritance to FALSE.
        0,                        // No creation flags.
        NULL,                  // Use parent's environment block.
        NULL,                  // Use parent's starting directory.
        &si,                  // Pointer to STARTUPINFO structure.
        &pi)                  // Pointer to PROCESS_INFORMATION structure.
    )
    {
        // ErrorExit( "CreateProcess failed." );
            ASSERT(false);
            return false;
    }

    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);

    // Close process and thread handles.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
      return true;
}

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey, that's fixed it, great!
It now works with the routine with the waiting also, and I added the creation flag CREATE_NO_WINDOW to make it hide.  [It says in the doc "This flag cannot be used with MS-DOS-based applications" but it seems to work fine??]

Thanks!