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