//+------------------------------------------------------------------+
//| Demo_FolderDelete.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//--- Description
#property description "The script shows a sample use of FolderDelete()."
#property description "First two folders are created; one of them is empty, the second one contains a file."
#property description "When trying to delete a non-empty folder, an error is returned and a warning is shown."
//--- Show the dialog of input parameters when starting the script
#property script_show_inputs
//--- Input parameters
input string firstFolder="empty"; // An empty folder
input string secondFolder="nonempty";// The folder, in which one file will be created
string filename="delete_me.txt"; // The name of the file that will be created in folder secondFolder
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Write the file handle here
int handle;
//--- Find out in what folder we are working
string working_folder=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files";
//--- A debug message
PrintFormat("working_folder=%s",working_folder);
//--- Trying to create an empty folder relative to path MQL5\Files
if(FolderCreate(firstFolder,0)) // 0 means that we are working in the local folder of the terminal
{
//--- Enter the full path to the created folder
PrintFormat("Folder %s has been created",working_folder+"\\"+firstFolder);
//--- Reset the error code
ResetLastError();
}
else
PrintFormat("Failed to create folder %s. Error code %d",working_folder+"\\"+firstFolder, GetLastError());
//--- Now create a non-empty folder using the FileOpen() function
string filepath=secondFolder+"\\"+filename; // Form path to file that we want to open to write in a nonexistent folder
handle=FileOpen(filepath,FILE_WRITE|FILE_TXT); // Flag FILE_WRITE in this case is obligatory, see Help for FileOpen
if(handle!=INVALID_HANDLE)
PrintFormat("File %s has been opened for reading",working_folder+"\\"+filepath);
else
PrintFormat("Failed to create file %s in folder %s. Error code=",filename,secondFolder, GetLastError());
Comment(StringFormat("Prepare to delete folders %s and %s", firstFolder, secondFolder));
//--- A small pause of 5 seconds to read a message in the chart
Sleep(5000); // Sleep() cannot be used in indicators!
//--- Show a dialog and ask the user
int choice=MessageBox(StringFormat("Do you want to delete folders %s and %s?", firstFolder, secondFolder),
"Deleting folders",
MB_YESNO|MB_ICONQUESTION); // Two buttons - "Yes" and "No"
//--- Run an action depending on the selected variant
if(choice==IDYES)
{
//--- Delete the comment form the chart
Comment("");
//--- Add a message into the "Experts" journal
PrintFormat("Trying to delete folders %s and %s",firstFolder, secondFolder);
ResetLastError();
//--- Delete the empty folder
if(FolderDelete(firstFolder))
//--- The following message should appear since the folder is empty
PrintFormat("Folder %s has been successfully deleted",firstFolder);
else
PrintFormat("Failed to delete folder %s. Error code=%d", firstFolder, GetLastError());
ResetLastError();
//--- Delete the folder that contains a file
if(FolderDelete(secondFolder))
PrintFormat("Folder %s has been successfully deleted", secondFolder);
else
//--- The following message should appear since the folder contains a file
PrintFormat("Failed to delete folder %s. Error code=%d", secondFolder, GetLastError());
}
else
Print("Deletion canceled");
//---
}
|