2016-09-14 10 views
0

私は現在、ネットワークデバイスのrobocopyバックアップを支援するための小さなプログラムを作成しようとしているC++の初心者です。これまでのところ、私は以下のコードを思いつきましたが、ユーザー定義の入力を伴うC++ Robocopy

31「『ROBOCOPY //』 < < OLDNAME」私はrobocopyをを使用して、すべての行に対して繰り返し同じエラーを取得し

で「演算子< <」の一致、任意のヘルプはないだろう:次のエラーを取得します大いに感謝します。始まるのおかげで、すべての

#include <iostream> 
#include <cstdlib> 
using namespace std; 



int main() 
{ 
     string oldname; 
     string newname; 
     string userid; 
     char response;   


     cout<<"Please input old device name eg XXXXXX\n"; 
     cin>> oldname; 

     cout<<"Please input new device name eg XXXXXX\n"; 
     cin>> newname; 

     cout<<"Please input userid eg SP12345\n"; 
     cin>> userid;     

     cout<<"Does your current device contain a D: drive? Y or N?"; 
     cin>> response; 

        if (response == 'Y') 
        { 

        std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/Desktop //"<<newname<<"/C$/Users/"<<userid<<"/Desktop /MIR /w:0 /r:0"); 
        std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/Favorites //"<<newname<<"/C$/Users/"<<userid<<"/Favorites /MIR /w:0 /r:0"); 
        std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/My Documents //"<<newname<<"/C$/Users/"<<userid<<"/My Documents /MIR /w:0 /r:0"); 
        std::system("ROBOCOPY //"<<oldname<<"/d$ //"<<newname<<"/C$/Users/"<<userid<<"/Desktop/D backup /MIR /w:0 /r:0"; 
         }   

        else if (response == 'N') 
        { 
         std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/Desktop //"<<newname<<"/C$/Users/"<<userid<<"/Desktop /MIR /w:0 /r:0; 
         std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/Favorites //"<<newname<<"/C$/Users/"<<userid<<"/Favorites /MIR /w:0 /r:0; 
         std::system("ROBOCOPY //"<<oldname<<"/c$/Users/"<<userid<<"/My Documents //"<<newname<<"/C$/Users/"<<userid<<"/My Documents /MIR /w:0 /r:0; 
         } 

       system("pause");   
} 

答えて

1

は、この単純な文では動作しません:

std::string str; 
system(str); //<== expecting C-string 

systemがnullで終わるC文字列ではなく、std::stringを想定しているため。テキストを追加することにより悪化させます:

system("text" + str); 

コンパイラはそれをどうしたらいいのか分かりません。

第2に、systemはコマンドラインパラメータを正しく渡すことができません。必要がありますCreateProcessまたはShellExecuteEx

また、アプリケーションのフルパスを渡す必要があります。例:

#include <iostream> 
#include <string> 
#include <sstream> 
#include <Windows.h> 

void foo(std::string userid, std::string oldname, std::string newname) 
{ 
    std::stringstream ss; 
    ss << "c:\\Program Files (x86)\\Full Path\\Robocopy.exe" 
     << " /c:\\users\\" << userid << "\\Desktop\\" << oldname 
     << " /c:\\users\\" << userid << "\\Desktop\\" << newname 
     << " /MIR /w:0 /r:0"; 

    std::string commandLine = ss.str(); 

    //examine the commandline! 
    std::cout << commandLine << "\n"; 

    STARTUPINFOA si; 
    PROCESS_INFORMATION pi; 
    memset(&si, 0, sizeof(si)); 
    si.cb = sizeof(si); 
    memset(&pi, 0, sizeof(pi)); 

    char *buf = _strdup(commandLine.c_str()); 
    CreateProcessA(0, buf, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); 
    free(buf); 
} 

int main() 
{ 
    foo("x", "y", "z"); 
    return 0; 
} 
関連する問題