mirror of
https://codeberg.org/1414codeforge/lua-osx.git
synced 2025-04-28 08:48:51 +02:00
[examples/rename] Improve comments.
This commit is contained in:
parent
ba26f107da
commit
ca5ea0085c
@ -21,6 +21,7 @@ local pattern = assert(select(1, ...), "Missing filename pattern argument")
|
|||||||
local repl = assert(select(2, ...), "Missing filename replacement argument")
|
local repl = assert(select(2, ...), "Missing filename replacement argument")
|
||||||
local path = select(3, ...) or "."
|
local path = select(3, ...) or "."
|
||||||
|
|
||||||
|
-- Rename en-masse
|
||||||
for filename in dir(path) do
|
for filename in dir(path) do
|
||||||
local newfilename = filename:gsub(pattern, repl, 1)
|
local newfilename = filename:gsub(pattern, repl, 1)
|
||||||
|
|
||||||
@ -28,14 +29,17 @@ for filename in dir(path) do
|
|||||||
local from = path..sep..filename
|
local from = path..sep..filename
|
||||||
local to = path..sep..newfilename
|
local to = path..sep..newfilename
|
||||||
|
|
||||||
|
-- Refuse to overwrite files
|
||||||
local buf, err, code = osx.stat(to)
|
local buf, err, code = osx.stat(to)
|
||||||
if buf then
|
if buf then
|
||||||
error(filename..": File '"..to.."' exists, refusing to overwrite.")
|
error(filename..": File '"..to.."' exists, refusing to overwrite.")
|
||||||
end
|
end
|
||||||
if code ~= ENOENT then
|
if code ~= ENOENT then
|
||||||
|
-- Anything but "file does not exist" is dangerous, so bail out
|
||||||
error(err)
|
error(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Print a nice message and rename things
|
||||||
write(from, ' -> ', to)
|
write(from, ' -> ', to)
|
||||||
assert(osx.rename(filename, newfilename))
|
assert(osx.rename(filename, newfilename))
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user