Solved

Windows System Command (xcopy) in my c++ code.

Posted on 2009-05-06
21
699 Views
Last Modified: 2013-12-03
Hi,

I am facing problem of using Windows System Command (xcopy) in my c++ code.
Please let me know how to do it. Do I need to create new process?


Do I need to use system() api
Thanks....
0
Comment
Question by:palhade
  • 10
  • 10
21 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24312456
Can you post your code please... we're good but no psychic :)
0
 

Author Comment

by:palhade
ID: 24312764
Following is the code i am trying to extecute.
It should override it if file already exist.
String            mExecutable = "xcopy";
String            mSourceFile = "C:\Temp\test.dat";
String            mDestDir = "C:\\";
String            str;
strArgs.format( "%s %s %s", mExecutable.Buffer(),mSourceFile.Buffer(),mDestDir.Buffer() );
system(str.Buffer());
0
 
LVL 6

Expert Comment

by:Mushq
ID: 24312946
>>Do I need to create new process?:
Yes I think you need to create a process there is an API CreateProcess for that. http://msdn.microsoft.com/en-us/library/ms682425.aspx

For simplicity you can use WinExec . Please check that.
http://msdn.microsoft.com/en-us/library/ms687393(VS.85).aspx

Regards,
Mushi
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24312996
>> Yes I think you need to create a process
No, you don't the system() function takes care of all this for you. As long as the command line string is well formed it'll just work. Your parent application will block until the system executed command has terminated.

http://www.cplusplus.com/reference/clibrary/cstdlib/system/

>> For simplicity you can use WinExec
Since xcopy is a console command you probably want to call system().
0
 

Author Comment

by:palhade
ID: 24313088
Yes i want to execute "xopy" command but in differnet process.
 
But in system() it halt parent application. This i don't want. Please help me about how to create new process.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24313125
>> But in system() it halt parent application. This i don't want. Please help me about how to create new process.
Take a look at spawnlp,. which can be executed asynchronously
http://msdn.microsoft.com/en-us/library/t7y6z6fd.aspx
0
 

Author Comment

by:palhade
ID: 24313414
Hi,
 
Thanks for your reply.
 
Can you please give me c++ code for to create new process & to execute "xopy" command inside it.
 
I tried it but unable to do it.
 
Thanks...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24313883
>> I tried it but unable to do it.
Paste what you have and I'll see why it doesn't work :)
0
 

Author Comment

by:palhade
ID: 24313970

PROCESS_INFORMATION sProcessInfo;
STARTUPINFO sStartupInfo;
ZeroMemory(&sStartupInfo,sizeof(STARTUPINFO));
ZeroMemory(&sProcessInfo,sizeof(PROCESS_INFORMATION));
sStartupInfo.cb = sizeof( STARTUPINFO);

BOOL result = ::CreateProcess(mExecutable .c_substr(),
0, 0, 0, 0,
CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS,
0, 0,
&sStartupInfo,
&sProcessInfo);
 
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24314000
Did you not look at spawnlp? CreateProcess is, IMHO, overkill for this. Can I suggest you look at that because using it is not much harder than system().
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:palhade
ID: 24314265
How to change above program into spawnlp().
 
please help me to change it
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24314712
Something like this (you'll need to tweak it to suit your needs).
#include <process.h>
 

int main()

{

	intptr_t res = _spawnlp(

		_P_NOWAIT,

		"xcopy.exe",

		"C:\\Temp\\test.dat",

		"C:\\",

		0 

	);

}

Open in new window

0
 

Author Comment

by:palhade
ID: 24322373
But 4th parameter ("C:\\",)i want defalut directory means current application directory. I do not want to specify "C:\\", is there any way to do so
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24322943
>> is there any way to do so
Have you tried just "." since the . is always current directory. Otherwise look at GetCurrentDirectory()
http://msdn.microsoft.com/en-us/library/aa364934(VS.85).aspx
0
 

Author Comment

by:palhade
ID: 24323144
hi,
i tried this but it is not working
intptr_t res = _spawnlp( _P_NOWAIT, "xcopy", "D:\tmep\temp.dat /Y", NULL, 0 );
0
 

Author Comment

by:palhade
ID: 24323208

hi,

i tried the way you told
intptr_t res = _spawnlp( _P_NOWAIT, "xcopy", "D:\tmep\temp.dat /Y", ".", 0 );
But i am getting following error
File cannot be copied onto itself
0 File<s> copied
 
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24323231
intptr_t res = _spawnlp(
 _P_NOWAIT,
"xcopy",
"D:\tmep\temp.dat /Y",      // Is the source path valid? Also where is the destination?
NULL,                                 // You don't need NULL and 0, either will do -- it's just a way of terminating the varargs
0 );

spawnlp is a variable argument function, with 0 or NULL terminating the varargs. Each part of the xcopy <source>, <dest>, <params> should really be a separate argument. Something like below.


#include <process.h>

#include <windows.h>
 

int main()

{

	char szCurWorkingDir[MAX_PATH] = {0};
 

	GetCurrentDirectoryA(

		MAX_PATH,

		szCurWorkingDir

		);
 
 

	intptr_t res = _spawnlp(

		_P_NOWAIT,

		"xcopy",

		" /Y",

		"D:\temp\temp.dat",

		szCurWorkingDir,

		NULL 

	);
 

}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24323236
>> File cannot be copied onto itself
Check that the xcopy command you are passing to spawnlp is valid by typing it into a command line console in the same working directory as where you are executing your code.
0
 

Author Comment

by:palhade
ID: 24323297
I check xcopy command it is working fine there.
I tried your code also but it is giving following error
 File cannot be copied onto itself
0 File<s> copied
What is the purpose of following code
char szCurWorkingDir[MAX_PATH] = {0};

GetCurrentDirectoryA(
MAX_PATH,
szCurWorkingDir
);
 
0
 

Author Comment

by:palhade
ID: 24323306

I check xcopy command it is working fine there.
I tried your code also but it is giving following error
 File cannot be copied onto itself
0 File<s> copied
What is the purpose of following code
char szCurWorkingDir[MAX_PATH] = {0};
GetCurrentDirectoryA(
MAX_PATH,
szCurWorkingDir
);
 
 
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 24323343
>> What is the purpose of following code
You said you wanted the current working directory right? This gets that.

>> File cannot be copied onto itself
The current working directory isn't the same directory as the file is in it is? This error suggests that is the case. What do you see if you do the following....?
#include <process.h>

#include <windows.h>

#include <iostream>

#include <cstdlib>
 

int main()

{

	char szCurWorkingDir[MAX_PATH] = {0};

	char szSource[] = "D:\\temp\\temp.dat";
 

	GetCurrentDirectoryA(

		MAX_PATH,

		szCurWorkingDir

		);
 

	// DEBUG

	std::cout << "Copying " << szSource << " to " << szCurWorkingDir << std::endl;

	system("pause");

	// DEBUG
 

	intptr_t res = _spawnlp(

		_P_NOWAIT,

		"xcopy",

		" /Y",

		szSource,

		szCurWorkingDir,

		NULL 

	);
 

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now