mp.file — 14 functions
| Function | Returns | Description |
|---|---|---|
load(path) | data, err | {blocks=[{address, data[], size}], block_count, populated=[{region, offset, bytes}]} — parses HEX/S19/BIN AND populates the matched regions in the buffer (raw .bin lands at active region offset 0). err is non-empty when target tabs are read-only |
load_bin(path) | bytes, err | Returns the file as a flat byte array AND populates the active region at offset 0 |
save(path, blocks) | ok, err | Save HEX/S19/BIN (format by extension). Calling without blocks returns (false, "use mp.memory.save_file(path)") |
save_bin(path, bytes) | ok, err | Save raw bytes to a .bin file |
read_text(path) | text, err | UTF-8 text |
write_text(path, s) | ok, err | Truncating write |
append_text(path, s) | ok, err | Append to file |
exists(path) | bool | — |
size(path) | int | Size in bytes (0 if missing) |
extension(path) | string | File suffix without dot |
basename(path) | string | Name without directory or extension |
dirname(path) | string | Directory path |
list_dir(path, filter?) | table | Files and subdirs; filter is a glob like "*.lua" |
detect_format(path) | string | "hex" | "s19" | "bin" | "unknown" |
Example
-- Parses HEX/S19/BIN AND populates the buffer at the matched addresses.
local data, err = mp.file.load("C:/fw.hex")
if err and err ~= "" then mp.log.error(err); return end
mp.log.info("blocks=" .. data.block_count)
-- Save to S19
mp.file.save("C:/out.s19", data.blocks)
-- Text helpers
mp.file.write_text("C:/log.csv", "serial,result\n")
mp.file.append_text("C:/log.csv", "SN001,OK\n")
-- List Lua scripts in a directory
for _, name in ipairs(mp.file.list_dir("C:/scripts", "*.lua")) do
mp.log.info(name)
end