- 5,970
- 4,295
описание: копирует все содержимое либо что-то одно на выбор по указанному пути, и копирует в указанный путь
код:
пример использования:
код:
Lua:
function copyPath(src, dst)
local function norm(p)
return p:gsub("/", "\\"):gsub("\\+", "\\"):gsub("\\+$", ""):lower()
end
local nsrc, ndst = norm(src), norm(dst)
if nsrc == ndst then
return nil, "source and destination paths are the same"
end
if doesDirectoryExist(src) and ndst:sub(1, #nsrc+1) == nsrc.."\\" then
return nil, "destination is inside source"
end
if doesFileExist(src) then
local name = src:match("[^\\]+$")
if doesDirectoryExist(dst) then
dst = dst .. "\\" .. name
elseif not doesFileExist(dst) then
local last = dst:match("[^\\]+$") or ""
if dst:sub(-1) == "\\" or not last:find("%.") then
if not doesDirectoryExist(dst) and not createDirectory(dst) then
return nil, "cannot create destination directory"
end
dst = dst .. "\\" .. name
end
end
local r, err = io.open(src, "rb"); if not r then return nil, "cannot open source: " .. tostring(err) end
local data = r:read("*a"); r:close()
local w; w, err = io.open(dst, "wb"); if not w then return nil, "cannot write destination: " .. tostring(err) end
w:write(data); w:close()
return true
end
if doesDirectoryExist(src) then
if doesFileExist(dst) then
return nil, "cannot copy directory into existing file"
end
if not doesDirectoryExist(dst) and not createDirectory(dst) then
return nil, "cannot create destination directory"
end
local h, f = findFirstFile(src.."\\*")
while f do
if f ~= "." and f ~= ".." then
local ok, err = copyPath(src.."\\"..f, dst.."\\"..f)
if not ok then
findClose(h)
return nil, err
end
end
f = findNextFile(h)
end
findClose(h)
return true
end
return nil, "source not found"
end
Lua:
function main()
while not isSampAvailable() do wait(0) end
-- создаст backup\\EVSP.ini
local ok, err = copyPath(getWorkingDirectory().."\\config\\EVSP.ini", getWorkingDirectory().."\\backup\\")
print(ok and "OK (файл - папка)" or "Ошибка: "..err)
-- создаст backup\\copy.ini
ok, err = copyPath(getWorkingDirectory().."\\config\\EVSP.ini", getWorkingDirectory().."\\backup\\copy.ini")
print(ok and "OK (файл - файл)" or "Ошибка: "..err)
-- создаст backup\\nested\\EVSP.ini
ok, err = copyPath(getWorkingDirectory().."\\config\\EVSP.ini", getWorkingDirectory().."\\backup\\nested\\")
print(ok and "OK (файл - новая папка)" or "Ошибка: "..err)
-- создаст backup_config\\(содержимое config)
ok, err = copyPath(getWorkingDirectory().."\\config", getWorkingDirectory().."\\backup_config")
print(ok and "OK (папка - папка)" or "Ошибка: "..err)
-- создаст backup\\config\\(содержимое config)
ok, err = copyPath(getWorkingDirectory().."\\config", getWorkingDirectory().."\\backup\\")
print(ok and "OK (папка - внутрь папки)" or "Ошибка: "..err)
wait(-1)
end
Последнее редактирование: