Loading…

Filename Sanitiser

A symbol name with a slash in it, a user-typed report title with a colon in a timestamp, or a caption pasted from somewhere else can all throw ERR_INVALID_FILENAME the moment they reach FileOpen(), or worse, silently land on a different file than the one the caller meant. Stripping "a few obvious symbols" by hand misses three traps that are specific to Windows rather than to MQL4: reserved device basenames (CON, PRN, AUX, NUL, COM1-9, LPT1-9 are illegal even with an extension attached, e.g. "con.txt"), trailing dots and spaces (Windows removes these silently at file-creation time, so a name that still has them does not round-trip to what was written), and the full block of ASCII control characters below 0x20, not just the nine punctuation characters most naive sanitisers remember. FsnSanitise runs all of this in one pass: replace every illegal or control character with an underscore, trim surrounding whitespace, strip trailing dots/spaces, fall back to a default name if that leaves nothing at all, defuse a reserved device basename by appending a suffix, and finally enforce a length cap that shortens the base rather than truncating the extension away. FsnSanitiseEx exposes the length cap and replacement character for callers who need a shorter budget (e.g. writing into an already-long destination path) or a different filler character. Notes and limitations Operates on a single filename component, not a full path - it does not touch path separators as structure, only as illegal characters within a name, so do not feed it a multi-segment path expecting the slashes to survive. The reserved- device-name check matches the classic Windows list (CON/PRN/AUX/NUL/COM1-9/ LPT1-9); it does not attempt to cover every quirk of every filesystem MetaTrader might run under. Truncation is character-count based (StringLen), not byte-count based, so a name built from characters outside the ASCII range that MetaTrader represents with more than one storage unit is not accounted for precisely - keep candidate names ASCII where the exact byte budget matters. The fallback name ("untitled") and replacement character ('_' by default) are deliberately plain; callers wanting different conventions should use FsnSanitiseEx directly.

Script · MT4 · v1.0 · Free

About Filename Sanitiser

Filename Sanitiser is a script for MetaTrader 4. It has 1 setting, such as Candidate name. MQL source code included.

Files in the download: FilenameSanitiser.mqh, FilenameSanitiser.txt, FilenameSanitiserDemo.mq4.

Settings

SettingDefaultWhat it does
Candidate nameReport: EURUSD/USDJPY <2024>.txtname to sanitise and test-write

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Trailing Stop & Break-even · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria