Loading…

Batch Rename Exported Files

Terminals that export CSV files - tick logs, trade history, custom indicator dumps - tend to leave them with whatever name the exporting code happened to give them, or a name that gets overwritten on the next run. Sorting them into a consistent, timestamped naming scheme by hand is the kind of small chore that never gets done. The trap is that MQL4 has no rename call at all. There is no FileRename() function on this platform. A script that wants to look like it renames a file has exactly two honest options: refuse, or build the effect out of FileCopy() followed by FileDelete() of the source. This script does the second, and says so at the point in the code where it happens, because that is a design fact worth knowing before you trust it with files you care about - the operation is a copy-then-delete, not a single atomic move at the operating system level. If the terminal is closed or crashes between the two calls, the old file can survive next to the new one, or, less likely, the delete can fail after the copy succeeds and leave a duplicate. This is also exactly why InpDryRun defaults to true: the full old-name to new-name mapping is written to the journal before either FileCopy or FileDelete is ever called, so you can check the plan against what you expected first. Each new name carries the source file's own last-modified date, not today's date, converted to an ISO-style stamp with the colons that Windows filenames forbid replaced by dots. A running sequence number is added on top so that several files sharing the same modify date - common with files exported in the same batch - still get distinct names. If a computed name happens to already exist on disk, or was already claimed by an earlier file in the same run, the script adds a letter suffix and tries again rather than overwriting anything. Notes and limitations - Operates only inside the sandbox (MQL4\Files or MQL5\Files, matching the platform) and its subfolders as matched by the wildcard. It cannot see or touch files anywhere else on the drive - that restriction belongs to the platform, not to this script. - A rename here is a copy followed by a delete, not a single atomic operating system rename. Between the two steps both the old and the new file can briefly exist together, and if the script is interrupted after the copy but before the delete succeeds, the old file is left behind rather than removed. The journal always names exactly which file that happened to, so nothing is silently lost - but it is not the same guarantee a native rename would give you. - Existing files are never overwritten. A name collision gets a letter suffix instead; it does not replace what was already there. - The modify date used is whatever FILE_MODIFY_DATE reports for the source file at the moment it is opened, not the date it is copied. - A failure on one file (for example, a file locked by another program) is skipped and reported; it does not stop the rest of the batch from being processed.

Script · MT5 · v1.0 · Free

About Batch Rename Exported Files

Batch Rename Exported Files is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Wildcard, Prefix and Dry run. MQL source code included.

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

Settings

SettingDefaultWhat it does
Wildcard*.csvFile pattern to match, in MQL5\Files
PrefixexportPrefix for the renamed files
Dry runtruePreview only - report, do not touch the filesystem

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