Loading…

Purge Old Files In Files Folder

The Files sandbox fills up quietly. Every export script, every log, every report a trading system writes lands there and nothing in the platform ever cleans it out. Left alone for a year it becomes a folder nobody wants to open, and clearing it by hand risks deleting something a running EA is still reading. The naive version of a cleanup script deletes on the first run and finds out afterwards what it should not have touched. This one defaults to a dry run: it lists every file that matches the age and extension filter, with its size and modified date, and writes that same list to a report CSV, without deleting a single byte. Only after you have looked at that list and switched InpDryRun off does anything get removed, and even then a confirmation dialog states the exact count and total size before the deletion runs. Two details matter for correctness. First, extension matching is done on the file's own real extension, checked exactly, rather than trusted to the platform's own find-first wildcard, which treats "*.csv" as a prefix match - that would also catch a file named export.csv1. Second, MQL has no direct way to ask "is this a directory" inside the sandbox on either platform; each entry is probed by trying to open it as a file. Success means a file to consider; failure means treat it as a folder and recurse into it if subfolder scanning is on. The report this script itself writes is excluded from every scan by its own filename prefix, so a script left running on a schedule never ends up purging its own output on the following pass. Every file's size and modified date is read once, up front, into a fixed list. Deletion, when it happens, works from that list afterwards rather than deleting while still walking the folder, which is the same "collect first, act after" principle that keeps a live folder scan from skipping entries as they disappear out from under it. Notes and limitations - Only files reachable under the terminal's own Files sandbox are ever touched - this script cannot see or delete anything outside it. - The "is this a folder" check works by attempting to open each entry as a file; an entry that cannot be opened for any reason (a genuine folder, but also rarely a file locked by another process) is treated as a folder and either recursed into or skipped, not reported as an error. - The report CSV this script writes is itself excluded from every scan by its filename prefix, so running it repeatedly on a schedule will not have it purge its own history of previous runs. - Deletion failures (a file locked by another program, for instance) are reported individually in the log and marked FAILED in the report CSV rather than stopping the run. - Always run a dry run first on a folder you have not cleaned before. There is no undo once InpDryRun is off and a file is actually removed.

Script · MT5 · v1.0 · Free

About Purge Old Files In Files Folder

Purge Old Files In Files Folder is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Max age days, Recurse subfolders and Dry run. MQL source code included.

Files in the download: PurgeOldFiles.ex5, PurgeOldFiles.mq5, PurgeOldFiles.txt.

Settings

SettingDefaultWhat it does
Max age days30Delete files last modified more than this many days ago
Recurse subfolderstrueAlso scan subfolders of MQL5\Files
Dry runtrueList what would be deleted, delete nothing
Confirm firsttrueLive run only: ask for confirmation before deleting

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria