2012-06-18 180 views
0

我想使用批處理文件來移動文件,因爲我使用下面的代碼,該過程似乎成功執行,但文件不會被傳輸。這段代碼有什麼問題嗎?在VC++中運行批處理文件

STARTUPINFO osi; 
    PROCESS_INFORMATION pi; 
    ZeroMemory(&osi, sizeof(osi)); 
    osi.cb = sizeof(capinfo); 
    ZeroMemory(&pi, sizeof(pi)); 

    wchar_t cmd[8192]; 
    wchar_t ocmd[8192]; 
    char ocmd1[8192]; 
    swprintf_s(ocmd,sizeof(ocmd),L"%s\\%s.bat",capodd->templocation,capodd->wcurrentoutput); 
    sprintf_s(ocmd1,sizeof(ocmd1),"%ls\\%s.bat",capodd->templocation,capodd->currentoutput); 
    FILE * tempbat = fopen(ocmd1,"w"); 

    swprintf_s(cmd,8192,L"move %s\\%s.mp4 %s\\%s.mp4",capodd->templocation,capodd->wcurrentoutput,capodd->destination,capodd->wcurrentoutput); 
    fprintf(tempbat,"%ls\n",cmd); 
    //swprintf_s(cmd,8192,L"move %s\\%s.txt %s\\%s.txt",capodd->templocation,capodd->currentoutput,capodd->destination,capodd->currentoutput); 
    //fprintf(tempbat,"%ls\n",cmd); 
    //fprintf(tempbat,"del %ls\n",ocmd); 
    fclose(tempbat); 
    swprintf_s(cmd,sizeof(cmd),L"cmd /c %s",ocmd); 
    // just for fun, sleep for twice my latency to make sure the other half of the graph has started 
    Sleep(1000 *2); // of course, this means our overlap better be within 2x of the periodicity 

    if (!CreateProcess(NULL,cmd,NULL,NULL,FALSE,0,NULL,NULL,&osi,&pi)) { 
     fprintf(capodd->c_pF,"couldn't execute copy %d\n",GetLastError()); 
    } 

回答

1

有您的代碼2個問題:
應改爲osi.cb = sizeof(osi);
2-使用sizeof()swprintf_s是不正確的。您應該使用_countof()代替它,或者在使用靜態數組時可以省略sizeof()
我測試了這段代碼,並沒有其他問題。

+0

謝謝@A。 Danesh爲你的回覆,實際上問題是該文件正在被另一個進程使用,而且它並沒有傳輸文件。 – meghana