读取.bat文件的相关内容


#include <windows.h>
#include <fstream>
#include <string>
using namespace  std;


bool RunBatFile(char* batFileName)
{
    char CurDir[MAX_PATH] = { 0 };
    char BatFilePath[MAX_PATH] = { 0 };
    GetModuleFileNameA(NULL, CurDir, sizeof(CurDir));
    ::PathRemoveFileSpecA(CurDir);
    sprintf_s(BatFilePath, sizeof(BatFilePath), ("%s\\%s"), CurDir, batFileName);
//     ShellExecuteA(NULL, "open", BatFilePath, "", NULL, SW_SHOW);  // 可以直接执行该语句,下面的代码就不需要了

    std::ifstream batFile;
    batFile.open(BatFilePath);

    if (!batFile.is_open())
    {
        cout << "Open bat file fail...";
        return false;
    }
    std::string strTempCmd("");
    while (getline(batFile, strTempCmd))
    {
        if ("PAUSE" == strTempCmd)
        {
            break;
        }
        cout << "Command is: " << strTempCmd << endl;

    }
    batFile.close();
    return true;
}
 


版权声明:本文为shigzhu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。