I want to use this cmd command
ROBOCOPY D:\folder1 D:\folder2 /S /E
with conditions to copy the contents of folder1 to folder2
if(i == 1)
and,
if(i == 2)
ROBOCOPY D:\folder3 D:\folder4 /S /E
to copy the contents of folder3 to folder4
what should i do?
The simplest way is to call the standard library function
system
: http://www.cplusplus.com/reference/cstdlib/system/If you need more flexibility, http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx
CreateProcess
is the thing to go for - the STARTUPINFO argument lets you do things like pass it custom input and capture its output too.You simply do this (using the
std::system()
function):Note that for string literals like
"D:\folder3"
, you'll need to escape'\'
characters, with another'\'
:"D:\\folder3"
.Or even two more, depending on the interpreting command shell (should work on windows cmd without doing so):
"D:\\\\folder3"
.The easier way though, is to use the simpler to write
'/'
character, that's accepted for specifying windows pathes lately as well.