Skip to main content

mp.file — 14 functions

FunctionReturnsDescription
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, errReturns the file as a flat byte array AND populates the active region at offset 0
save(path, blocks)ok, errSave HEX/S19/BIN (format by extension). Calling without blocks returns (false, "use mp.memory.save_file(path)")
save_bin(path, bytes)ok, errSave raw bytes to a .bin file
read_text(path)text, errUTF-8 text
write_text(path, s)ok, errTruncating write
append_text(path, s)ok, errAppend to file
exists(path)bool
size(path)intSize in bytes (0 if missing)
extension(path)stringFile suffix without dot
basename(path)stringName without directory or extension
dirname(path)stringDirectory path
list_dir(path, filter?)tableFiles 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