在VC ++中运行批处理文件

时间:2012-06-18 14:57:17

标签: batch-file visual-c++ file-io

我想使用批处理文件移动文件,因为我确实使用下面的代码,过程似乎成功执行,但文件没有被转移。这段代码有什么问题吗?

    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 个答案:

答案 0 :(得分:1)

您的代码中存在两个问题:
1- osi.cb = sizeof(capinfo);应更改为osi.cb = sizeof(osi);
2-在sizeof()中使用swprintf_s不正确。您应该使用_countof()代替它,或者在使用静态数组时可以省略sizeof() 我测试了这段代码,没有其他问题。