#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <algorithm>
#include <fstream>
#include <random>
//for windows. this is not license for any person.
bool CreateText(const std::string& Name, const std::string& Text) {
std::ofstream ofs(Name);
if (ofs.is_open()) { ofs << Text << std::endl; }
return ofs.is_open();
}
int main(int ac, char* av[]) {
std::string Tag = "ODaI_0xdeadbeef.";
std::string Tag2 = "Odai_Killtask.";
std::time_t T = std::time(nullptr);
std::tm TM = *std::localtime(&T);
int PodS = 1;
std::string SH = std::to_string((TM.tm_hour + (TM.tm_min + PodS) / 60) % 24);
std::string SM = std::to_string((TM.tm_min + PodS) % 60);
if (SH.size() == 1) { SH = '0' + SH; };
if (SM.size() == 1) { SM = '0' + SM; };
int PodE = PodS + 11;
std::string EH = std::to_string((TM.tm_hour + (TM.tm_min + PodE) / 60) % 24);
std::string EM = std::to_string((TM.tm_min + PodE) % 60);
if (EH.size() == 1) { EH = '0' + EH; };
if (EM.size() == 1) { EM = '0' + EM; };
int PodJS = PodE + 2;
std::string JSH = std::to_string((TM.tm_hour + (TM.tm_min + PodJS) / 60) % 24);
std::string JSM = std::to_string((TM.tm_min + PodJS) % 60);
if (JSH.size() == 1) { JSH = '0' + JSH; };
if (JSM.size() == 1) { JSM = '0' + JSM; };
int PodJE = PodJS + 11;
std::string JEH = std::to_string((TM.tm_hour + (TM.tm_min + PodJE) / 60) % 24);
std::string JEM = std::to_string((TM.tm_min + PodJE) % 60);
if (JEH.size() == 1) { JEH = '0' + JEH; };
if (JEM.size() == 1) { JEM = '0' + JEM; };
std::random_device rd;
std::string BasePath = "c:\\Testdel\\";
std::string Name =std::to_string(std::time(nullptr)) + "_" + std::to_string(rd()) + ".tmp.bat";
//std::string Command = "rm -f ";//+av[0];
std::string Command = "dir";
std::string TN1 = "/tn \"" + Tag + "\" ";
std::string TN2 = "/tn \"" + Tag2 + "\" ";
std::string KillTask1 = "schtasks /delete " + TN1 + " /f \n";
std::string KillTask2 = "schtasks /delete " + TN2 + " /f \n";
std::string KillTask3 = "del "+Name+"\n";
std::string Schedule = "schtasks /create /tn \"" + Tag + "\" /tr \"" + Command + "\" /sc once /st " + SH + ':' + SM + " /et " + EH + ':' + EM + " /z";
bool F = CreateText(BasePath+Name, KillTask1 + KillTask2+KillTask3);
std::string Schedule2 = "schtasks /create /tn \"" + Tag2 + "\" /tr \"" + Name + "\" /sc once /st " + JSH + ':' + JSM + " /et " + JEH + ':' + JEM + " /f /z";
std::cout << "「このプログラムが起動するのは一度きりである。実行ののち、消去される。」" << std::endl;
std::system(Schedule.data());
std::system(Schedule2.data());
//std::system((Name).data());
if (!F) {
std::system(KillTask1.data());
std::system(KillTask2.data());
}
return 0;
}