Path And Filename Helper
MQL4 and MQL5 programs both live inside a sandbox: everything reachable is under MQL4\Files\ or MQL5\Files\ (or Common\Files\ with FILE_COMMON), so there is no drive letter, no absolute path and no ".." to resolve. What is left is pure string work - pulling a filename apart into its pieces and putting pieces back together - and that is all this header does. The extension functions use the LAST dot in the name, not the first, so "report.v2.csv" correctly yields ".csv" rather than ".v2.csv". A dot as the very first character of a name, the classic dotfile convention, is not treated as an extension at all, since there is no name in front of it to attach one to. PathCombine joins two path pieces with exactly one backslash between them no matter which side already has a separator - "reports" + "daily", "reports\" + "daily" and "reports" + "\daily" all produce the same result, which is the property that makes it safe to build paths from variables without checking each one for a trailing or leading slash first. PathSanitizeFileName is the function most of these exist to support. A filename assembled from a symbol name, an account comment, or any other value that came from outside the program can contain a character Windows will not accept - a colon, a question mark, a forward slash. FileOpen() then fails with a generic, unhelpful error. This function replaces every illegal character with a safe substitute and trims the trailing dots and spaces Windows also rejects, so a filename built from live data is guaranteed usable before the first FileOpen() call rather than discovered broken after it. The header is self-contained. No dependencies, no DLLs, no classes - plain functions you can call from a script, an indicator or an EA. Notes and limitations - This is string manipulation only. Nothing here touches the filesystem or checks whether a path actually exists - see FileHelper for that. - PathSanitizeFileName targets Windows filename rules specifically, since that is the only platform the MetaTrader sandbox runs on on either version. - PathCombine always joins with a backslash. It does not attempt to detect or preserve forward slashes from the input, since the sandbox itself is a Windows path underneath regardless of which separator a caller used.
Script · MT5 · v1.0 · Free
About Path And Filename Helper
Path And Filename Helper is a script for MetaTrader 5. MQL source code included.
Files in the download: PathUtils.mq5.mqh, PathUtils.txt, PathUtilsDemo.ex5, PathUtilsDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code