Search code examples
cmdshellexecutemql5shellexecuteex

cmd command in ShellExecuteW


I want to delete a file using cmd comment. I run the code in MQL5 language from my EA. Here's the code that I used

string PathReportBacktest=sTerminalTesterDataPath+"\\MQL5\\Files\\Reports\\"+StrategyCategory+"\\"+StrategyName+"\\"+ FileName +" Report.xml";

ShellExecuteW(0, "open", "cmd", "/C del "+PathReportBacktest, "", 0)

I tried to use the show window option and I found that the cmd opens up and closed without doing anything.


Solution

  • 2 things that I do to fix the problem,

    • I found the problem which is the space within the file, if the file name is text.txt (doesn't have space) it works with the above but if the file name is text 1.txt, it will fail to delete the file.

    • instead of using this code ShellExecuteW(0, "open", "cmd", "/C del "+PathReportBacktest, "", 0)

      I change it into ShellExecuteW(0, "open", "cmd", "/C del [fileName]", "[file location]", 0)

      e.g. ShellExecuteW(0, "open", "cmd", "/C del text*1.txt", "D:\\folder1\\", 0)