C++调用bat并实现传值
1.设置环境变量,这一类为路径
C++
void bat(const string& sDirC, const string& sDirD) { char Ddir[256]; Ddir[0] = 0; sprintf_s(Ddir, "DDIR=%s%s", (char*)sDirD.data(), "\\"); _putenv(Ddir); //将地址设置到当前环境中 char Cdir[256]; Cdir[0] = 0; sprintf_s(Cdir, "CDIR=%s%s", (char*)sDirC.data(), "\\"); _putenv(Cdir); system("..\\..\\Bin\\x64\\copy.bat" ); //调用bat 推荐下面那种 /*if (WinExec("..\\..\\Bin\\x64\\copy.bat", SW_HIDE) < 32) { AfxMessageBox(L" 无法调用 ..\\..\\Bin\\x64\\copy.bat", NULL, MB_OK); return false; }*/ }
Bat
set tmpDir=%DDIR% md %tmpDir% if not exist %tmpDir% ( set tmpDir=%CDIR% md %tmpDir% )
2.直接传值
bat默认只能获取到1-9个参数,分别用%1 %2 ... %9引用,如果传给bat的参数大于9个,就要用shift。
void bat(const CString& csVersion) { char packCmd[256]; sprintf_s(packCmd, "%s%s","..\\..\\Bin\\x64\\pack.bat ", T2A(csVersion)); //system(packCmd); if (WinExec(packCmd, SW_HIDE) < 32) { AfxMessageBox(L"..\\..\\Bin\\x64\\pack.bat ", NULL, MB_OK); return false; } }
set abc=XXX%1