1

I am writing this code in C language where I first find out the user logged in and then from that user's AppData, I need to copy some files. I am able to find, user, I am able to generate path, but the thing is I don't know how to copy a folder and its content using C so I thought of using System() commands. But now if I use COPY command, it says that path is incorrect, while actually it is correct and works fine if I use the same command on CMD. Also if I use XCOPY it says that the command is not recognized as an Internal or external command while XCOPY works fine on CMD.

So can someone tell me how can I actually copy the folder and its content?

I am shring the parts of codes to generate the file path and copy command.

//making path variable
char path[100]; 
strcat(path,"C:\\Users\\");
strcat(path,username); //username is variable it gets value from function

strcat(path,"\\AppData\\Local\\Google\\Chrome\\*.*");
printf(path);


char command[100]; 
strcat(command,"copy ");
strcat(command,path);
strcat(command," D:\\myFolder");
printf("\n");
printf(command);
printf("\n");
system(command);

UPDATE

Here is my complete Code, can somebody make this work?

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
#include <Lmcons.h>

int main()

{
//getting current user
TCHAR username[UNLEN+1];
DWORD len = UNLEN+1;
GetUserName(username, &len);
printf(username);
printf("\n");

//making path variable
char path[100]; 
strcpy(path,"C:\\Users\\");
strcat(path,username);

strcat(path,"\\AppData\\Local\\Google\\Chrome\\*.*");
printf(path);




//listing dir
DIR *dfd = opendir(path);
struct dirent *dp;
if(dfd != NULL) {
    while((dp = readdir(dfd)) != NULL)
        printf("%s\n", dp->d_name);
    closedir(dfd);
}

char command[100]; 
strcpy(command,"copy ");
strcat(command,path);
strcat(command," D:\\myFolder\\");
printf("\n");
printf(command);
printf("\n");
//sprintf(command, "copy %s/*.* D:/myfolder",path);
system(command);




return 0;
}
  • Not all PCs have a C: drive. – SLaks Mar 25 '13 at 18:05
  • Thankyou very much for pointing that out as well, I will also include to check for Windows Install Directory, but at the moment the windows drive is C, but I am having the problem mentioned above. – user2192499 Mar 25 '13 at 18:14
  • Same. fcopy is not recognized as an internal or external command. – user2192499 Mar 25 '13 at 18:15
  • I meant this: http://stackoverflow.com/questions/1006797/tried-and-true-simple-file-copying-code-in-c – McKracken Mar 25 '13 at 18:16
  • shouldn't there be something simpler? – user2192499 Mar 25 '13 at 18:24
  • You should consider using [CopyFile](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx) or one of its relatives. – zwol Mar 25 '13 at 19:05
  • Please don't refer to the `system` function as `System`. C is case-sensitive. When you write `System`, we can probably assume you mean `system`, but in fact they're two entirely distinct identifiers. – Keith Thompson Mar 25 '13 at 19:33

1 Answers1

1

You are using uninitialized arrays.

Change

strcat(path,"C:\\Users\\");
strcat(command,"copy ");

To

strcpy(path,"C:\\Users\\");
strcpy(command,"copy ");
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • I think the issue still is, I cannot copy all files and folders with COPY command, and XCOPY does not work, so what should I do? – user2192499 Mar 26 '13 at 07:22
  • `copy` can't copy directory. Only files. In fact, it is a command of the `CMD.exe` shell, and not an executable. As long as your are using `system`, that is fine. `xcopy.exe` is a program and you can execute it. Check `COMSPEC` and `PATH` environment variables. http://msdn.microsoft.com/en-us/library/277bwbdz%28v=vs.100%29.aspx – Valeri Atamaniouk Mar 26 '13 at 09:25