?
Solved

Sorting an external file from MFC app

Posted on 2004-11-08
4
Medium Priority
?
265 Views
Last Modified: 2010-04-17
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.

0
Comment
Question by:DaveB080597
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12532688
Try with WinExec:

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

Pay attention to file paths.

Good luck,
Jaime.
0
 

Author Comment

by:DaveB080597
ID: 12533025
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12533078
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
 

Author Comment

by:DaveB080597
ID: 12533210
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question