MQLArticles/Utils/File/FolderOps/FolderClean.mqh

98 lines
3.4 KiB
MQL5
Raw Permalink Normal View History

2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| FolderClean.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERCLEAN_MQH
#define MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERCLEAN_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Base.mqh"
2026-03-27 12:02:42 -05:00
// Com un folder clean pero con posibiad de excluir etc files
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-03-27 12:02:42 -05:00
class CFolderOpsClean : public CFolderOpsBase
2026-03-27 09:40:26 -05:00
{
2026-03-28 07:46:34 -05:00
private:
bool m_clean_all;
2026-03-27 09:40:26 -05:00
public:
2026-03-28 07:46:34 -05:00
CFolderOpsClean(void) : m_clean_all(false) {}
2026-03-27 12:02:42 -05:00
~CFolderOpsClean(void) {}
2026-03-27 09:40:26 -05:00
//---
// common flag = bandera de common FILE_COMMON si el dir se ubica en common de lo contrario 0
2026-03-28 07:46:34 -05:00
void SetInitialPass() { m_clean_all = false; }
2026-03-27 09:40:26 -05:00
bool CleanFolder(const string& init_dir, int common_flag);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-03-27 12:02:42 -05:00
bool CFolderOpsClean::CleanFolder(const string &init_dir, int common_flag)
2026-03-27 09:40:26 -05:00
{
//---
string filename = "";
::ResetLastError();
long handle = FileFindFirst(init_dir + "*", filename, common_flag);
if(handle == INVALID_HANDLE)
{
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
return true;
}
//---
do
{
2026-03-28 07:46:34 -05:00
const string full_filename = init_dir + filename;
2026-03-27 09:40:26 -05:00
//---
if(filename[StringLen(filename) - 1] == '\\')
{
2026-03-28 07:46:34 -05:00
//--- Buscamos si este folder esta en los arc hivos incluidos si lo esta entonces limpiamos todo sin exepcion
if(IsFileIncluyed(filename))
m_clean_all = true;
//--- Limpieza
if(!CleanFolder(full_filename, common_flag))
2026-03-27 09:40:26 -05:00
{
LogError(StringFormat("Error al limpiar el folder = %s", filename), FUNCION_ACTUAL);
2026-03-28 21:46:51 -05:00
FileFindClose(handle);
2026-03-27 09:40:26 -05:00
return false;
}
2026-03-28 07:46:34 -05:00
m_clean_all = false;
2026-03-27 09:40:26 -05:00
}
else
{
2026-03-28 07:46:34 -05:00
//---
if(!m_clean_all)
2026-03-27 09:40:26 -05:00
{
2026-03-28 07:46:34 -05:00
if(IsFileExcluyed(filename))
continue;
if(!IsFileIncluyed(filename))
continue;
}
//---
::ResetLastError();
if(!::FileDelete(full_filename, common_flag))
{
LogWarning(StringFormat("Fallo al eliminar el archivo = '%s', ultimo error = %d", filename, ::GetLastError()), FUNCION_ACTUAL);
2026-03-27 09:40:26 -05:00
}
}
}
while(FileFindNext(handle, filename));
2026-03-28 21:46:51 -05:00
//---
FileFindClose(handle);
2026-03-28 07:46:34 -05:00
2026-03-27 09:40:26 -05:00
//--- Si se limpio
return true;
}
//+------------------------------------------------------------------+
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERCLEAN_MQH