Hello all,

 I am working on a project and i need to basically copy a directory and its files to another location. I have figured out that xcopy comes in handy for what i want to do.but somehow when incorporated into my C code, it does not work. The code is this: 


     strcpy(src,"c:\\YeSQL\\");
     strcat(src,"db1");
     strcat(src,"\\*");
     strcpy(dest,"c:\\YeSQLBackUp");
     strcpy(command,"xcopy ");
     strcat(command,src);
     strcat(command," ");
     strcat(command,dest);
     strcat(command," \\s \\i \\q");
     printf("\n\n\n%s",command);
     system(command);

xcopy is an old old MS-DOS command, not implemented on MS-Windows 7/8 (don't know about previous versions of MS-Windows).

You might consider win32 api function SHFileOperations or the simpler to use and understand CopyFile along with FindFirstFile and FindNextFile to get the list of folders and files to be copied.

Actually, it's in Windows 7. Open a command window and type xcopy /? for all the info on it. Obviously, naming the files you want to move, and where you want to move them to, is critical.

That being said, I believe you'll enjoy programming it in C, instead, and learn something new in the process.

Yea, you are right. When I tried it I mis-read the error message, thinking it said "command not found". That's what I get for not reading more carefully.

Thank you for your inputs Ancient Dragon and Adak. I figured out the mistake in my code.
I just had to replace
strcat(command," \\s \\i \\q");
with
strcat(command," /s /i /q");

That part is done. now i have stumbled on another problem.
when the files already exist and i run the same function again, i get a prompt from xcopy asking me if i want to rewrite. I want to suppress this prompt.
I already tried

strcat(command," /s /i /q /y");

But it is not working. Could you give me a better option to supress this prompt?

Thank you!

It's a little sneaky, but you could pipe an answer to xcopy. It doesn't suppress the prompt, but it does execute it automatically:

echo F | xcopy /s /i /q /y src dst

thank you deceptikon, did not know that was even possible. Thanks a lot. but i got it workinng without echo. strcat(command," /s /i /q /y"); worked. It was my negligence not to have checked it properly before posting it here. Sorry, if this was an inconvinience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.