//+------------------------------------------------------------------+
//| 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); // 「はい(Y)」 と 「いいえ(N)」 の 2 つのボタン
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;
}
|