2

I would like to copy a file to a destination by using batch command in my code.

    szCommand.Format(_T("copy \"%s\" \"%s\""), szOrg, szTargetFile);
    _wsystem(szCommand);

However, each time _wsystem is called, a console window will be prompted, which is very unpleasant.

Is there a way to call _wsystem without showing out the console window? Or any other alternative?

wengseng
  • 1,330
  • 1
  • 14
  • 27

3 Answers3

7

To exert control over how a new program appears, use CreateProcess. Then you can use the CREATE_NO_WINDOW process-creation flag to hide the window of a console program.

But to copy a file from one place to another, skip the external programs and just call CopyFile.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    Or [`SHFileOperation`](http://msdn.microsoft.com/en-us/library/bb762164.aspx) with `FO_COPY`, if a nice graphical progress display is desired. – ephemient Jan 12 '11 at 06:57
1

Why shell-out when there's a Win32 API that will copy a file for you. It's called CopyFile!

Details here: http://msdn.microsoft.com/en-us/library/aa363851%28VS.85%29.aspx

#include <windows.h>

CopyFileA(szOrg, szTargetFile, FALSE);  // use CopyFileW if szOrg and szTargetFile are unicode strings
selbie
  • 100,020
  • 15
  • 103
  • 173
0

Would having the window minimized be okay? See this.

Community
  • 1
  • 1
wallyk
  • 56,922
  • 16
  • 83
  • 148