//+------------------------------------------------------------------+
//| Demo_FolderClean.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"
//--- 描述
#property description "The script shows a sample use of FolderClean()."
#property description "First, files are created in the specified folder using the FileOpen() function."
#property description "Then, before the files are deleted, a warning is shown using MessageBox()."
//--- 启动脚本时显示输入参数对话框
#property script_show_inputs
//--- 输入参数
input string foldername="demo_folder"; // 在MQL5/Files/创建一个文件夹
input int files=5; // 要创建和删除的文件数量
//+------------------------------------------------------------------+
//| 脚本程序启动函数 |
//+------------------------------------------------------------------+
void OnStart()
{
string name="testfile";
//--- 首先在程序端数据文件夹打开或创建文件
for(int N=0;N<files;N++)
{
//--- 'demo_folder\testfileN.txt'格式的文件名称
string filemane=StringFormat("%s\\%s%d.txt",foldername,name,N);
//--- 打开用于编写的标识文件,在这种情况下将会自动创建'demo_folder'
int handle=FileOpen(filemane,FILE_WRITE);
//--- 找出FileOpen()函数是否成功
if(handle==INVALID_HANDLE)
{
PrintFormat("Failed to create file %s. Error code",filemane,GetLastError());
ResetLastError();
}
else
{
PrintFormat("File %s has been successfully opened",filemane);
//--- 打开的文件不再需要,所以请关闭它
FileClose(handle);
}
}
//--- 检查文件夹中的文件数量
int k=FilesInFolder(foldername+"\\*.*",0);
PrintFormat("Totally the folder %s contains %d files",foldername,k);
//--- 显示询问用户的对话框
int choice=MessageBox(StringFormat("You are going to delete %d files from folder %s. Do you want to continue?",foldername,k),
"Deleting files from the folder",
MB_YESNO|MB_ICONQUESTION); // 两个按钮 - "Yes" 和 "No"
ResetLastError();
//--- 根据选定的变量运行一个行为
if(choice==IDYES)
{
//--- 开始删除文件
PrintFormat("Trying to delete all files from folder %s",foldername);
if(FolderClean(foldername,0))
PrintFormat("Files have been successfully deleted, %d files left in folder %s",
foldername,
FilesInFolder(foldername+"\\*.*",0));
else
PrintFormat("Failed to delete files from folder %s. Error code %d",foldername,GetLastError());
}
else
PrintFormat("Deletion canceled");
//---
}
//+------------------------------------------------------------------+
//| 在指定文件夹返回文件数量 |
//+------------------------------------------------------------------+
int FilesInFolder(string path,int flag)
{
int count=0;
long handle;
string filename;
//---
handle=FileFindFirst(path,filename,flag);
//--- 如果至少一个文件被找到,请搜索更多的文件
if(handle!=INVALID_HANDLE)
{
//--- 显示文件名称
PrintFormat("File %s found",filename);
//--- 增加找到的文件/文件夹的计数器
count++;
//--- 在所有文件/文件夹中开始搜索
while(FileFindNext(handle,filename))
{
PrintFormat("File %s found",filename);
count++;
}
//--- 完成时不要忘记关闭搜索句柄
FileFindClose(handle);
}
else // 获得句柄失败
{
PrintFormat("Files search in folder %s failed",path);
}
//--- 返回结果
return count;
}
|