File System Helper
The platform's file API is low level by design: open a handle, loop reading or writing, close the handle. That is fine once. Repeated across a project it becomes ten near-identical copies of the same loop, and the failure mode when one of those copies forgets to close on an early return is a locked sandbox file that blocks every future write until the terminal restarts - a bug that is invisible until the file is needed again, sometimes days later. This header collects the common shapes of that loop into single calls: whole file read and write, line-array read and write, and single-line append. Every function opens exactly one handle and closes it on every return path, including the early-failure ones, so the leaked-handle class of bug cannot occur through this header regardless of what the caller does with the result. Text is handled two ways deliberately. The line-based functions use FILE_TXT mode with FileWrite/FileReadString, the pairing the platform uses internally for FILE_TXT line records - correct for CSV rows, log lines, config files. The whole-text functions instead open FILE_BIN and move raw bytes, because FILE_TXT mode silently normalises line endings on write and can alter content that is not plain single-byte text. WriteAllText also trims the trailing zero terminator that StringToCharArray appends for C-string interop, so a binary write never leaves a stray NUL byte at the end of the file. Append is supported on both the line and the text functions. Neither platform has a dedicated append-open mode; FILE_WRITE alone always truncates. The append path here opens for read and write together and seeks to the current end before writing, which is the only way to append without reading the whole file back into memory first. 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 - All paths are sandboxed on both platforms. A program can only reach MQL4\Files\ or MQL5\Files\ (or Common\Files\ with FILE_COMMON) - there is no way to read or write anywhere else on disk, by design of the platform. - FileHelperEnsureFolder is best-effort. FolderCreate()'s return value does not reliably distinguish "already existed" from "failed" across terminal builds, so treat a false return as "verify manually" rather than a hard error - the next FileOpen() into the folder will surface a genuine problem regardless. - FileHelperCopy uses the same commonFlag for both source and destination; moving a file between the private sandbox and the common folder in one call is not something the underlying FileCopy() supports on either platform. - No file locking beyond what the platform already provides. Two programs writing the same file at the same time will interleave unpredictably, same as raw FileOpen/FileWrite would.
Script · MT5 · v1.0 · Free
About File System Helper
File System Helper is a script for MetaTrader 5. It has 1 setting, such as File name. MQL source code included.
Files in the download: FileHelper.mq5.mqh, FileHelper.txt, FileHelperDemo.ex5, FileHelperDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| File name | FileHelperDemo_scratch.txt | Sandbox file used for the demo |
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