How to copy files without displaying the dos window

I have the following code for copying files

sprintf(command, "copy /Y %s %s", sourceFile, targetFile);
system(command);

It works, except for the dos window, which is very annoying.

I am trying to use CreateProcess () (C # ifdef for WINNT), but not sure how to configure the command line for this. Any other options for copying files in C (on windows) without displaying the dos window?

+3
source share
5 answers

Windows provides an CopyFileAPI family for this.

+7
source

Here is the code I shot from this site . You can move it to its own function and simply pass the path of the source and target file (in this example, argv[1]and argv[2)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen(argv[1], "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen(argv[2], "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

  return 0;
}
+2
source

There are libraries that can do this, or you yourself can write the code using the buffer and fread / fwrite. I wrote C code for a long time, so I can’t remember the exact syntax.

0
source
    #include<windows.h>
    #include<tchar.h>
    #include<shellapi.h>


    #define _UNICODE

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
            PSTR szCmdLine, int iCmdShow)
    {

        SHFILEOPSTRUCT s={0};

        s.hwnd = GetFocus();
        s.wFunc = FO_COPY;
        s.pFrom = _T("d:\\songs\\vineel\\telugu\0\0");
        s.pTo = _T("d:\0");
        s.fFlags = 0;
        s.lpszProgressTitle = _T("Vineel From Shell - Feeling the power of WIN32 API");

        SHFileOperation(&s);
    }

In the above code, the Explorer Explorer handler will be called .....

0
source

Source: https://habr.com/ru/post/1744497/


All Articles