Failed to delete file using ShellExecuteW

 

Hi, I want to delete file using ShellExecuteW like code below but failed to launch cmd to delete the file. Anyone knows the problem?

#import  "shell32.dll"
int ShellExecuteW(int hwnd, string Operation, string
                  File, string Parameters, string Directory, int ShowCmd);
#import

// Deletion in Instance Optimize
   string PathReportBacktest=sTerminalTesterDataPath+"\\MQL5\\Files\\Reports\\"+StrategyCategory+"\\"+StrategyName+"\\"+ FileName +" Report.xml";
   string PathReportForward=sTerminalTesterDataPath+"\\MQL5\\Files\\Reports\\"+StrategyCategory+"\\"+StrategyName+"\\"+ FileName +" Report.forward.xml";

   ShellExecuteW(0, "Open", "cmd.exe", "/C del "+PathReportBacktest, "", 0);
   ShellExecuteW(0, "Open", "cmd.exe", "/C del "+PathReportForward, "", 0);
 
Why hassle with DLL calls? If you want to delete files in the normal "Files" sandbox, just use FileDelete.
 
It's not in normal "Files" sandbox though. I have 2 MT5 one for trading the other for optimization (not always active). I want to delete files from the MT5 optimization.
 

https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew

If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. 

the return value of my ShellExecuteW code is 42, according to documentation the code is success but the file isn't deleted.

 

I found a solution. I change the code into this:

string ReportBacktestName = "/C del ATR_X_CCIColorLevel*"+CurrentSymbol+"*"+tradeDirection+"*Report.xml";
string BacktestLocationPath = sTerminalTesterDataPath+"\\MQL5\\Files\\Reports\\"+StrategyCategory+"\\"+StrategyName+"\\";

ShellExecuteW(0, "open", "cmd.exe", ReportBacktestName, BacktestLocationPath, 0);

note this part.

Reason: