[examples/rename] Improve comments.

This commit is contained in:
Lorenzo Cogotti 2022-12-15 09:38:16 +01:00
parent ba26f107da
commit ca5ea0085c
1 changed files with 4 additions and 0 deletions

View File

@ -21,6 +21,7 @@ local pattern = assert(select(1, ...), "Missing filename pattern argument")
local repl = assert(select(2, ...), "Missing filename replacement argument")
local path = select(3, ...) or "."
-- Rename en-masse
for filename in dir(path) do
local newfilename = filename:gsub(pattern, repl, 1)
@ -28,14 +29,17 @@ for filename in dir(path) do
local from = path..sep..filename
local to = path..sep..newfilename
-- Refuse to overwrite files
local buf, err, code = osx.stat(to)
if buf then
error(filename..": File '"..to.."' exists, refusing to overwrite.")
end
if code ~= ENOENT then
-- Anything but "file does not exist" is dangerous, so bail out
error(err)
end
-- Print a nice message and rename things
write(from, ' -> ', to)
assert(osx.rename(filename, newfilename))
end