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.

DaveB080597Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
Try with WinExec:

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

Pay attention to file paths.

Good luck,
Jaime.
0
DaveB080597Author Commented:
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;
}

0
Jaime OlivaresSoftware ArchitectCommented:
ok, I remember now, echo and redirection is a feature of the command line. So you have to use the "cmd.exe"

::WinExec("cmd /c sort <myfile.txt >mysortedfile.txt", SW_HIDE);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveB080597Author Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.