Sorting an external file from MFC app

Posted on 2004-11-08
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.

Question by:DaveB080597
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Try with WinExec:

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

    Pay attention to file paths.

    Good luck,

    Author Comment

    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;

        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." );
                return false;

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

        // Close process and thread handles.
          return true;

    LVL 55

    Accepted Solution

    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);

    Author Comment

    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??]


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
    This is about my first experience with programming Arduino.
    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 …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now