Nextcloud 25中使用Lua脚本实现文件移动的技术求助
Nextcloud 25 文件移动 Lua 脚本示例
你提供的脚本存在多处问题:变量名不匹配、递归调用自身、未使用 Nextcloud 官方 Lua API 等。以下是适配 Nextcloud 25 的可用脚本示例:
-- 移动单个文件到指定目录 function file_move(file_node, target_dir_node) -- 检查文件和目标目录是否有效 if not file_node or not target_dir_node or not target_dir_node:isDirectory() then return nil, "无效的文件或目标目录" end -- 获取文件名并构建目标路径 local file_name = file_node:getName() local target_path = target_dir_node:getPath() .. "/" .. file_name -- 调用 Nextcloud 文件操作 API 执行移动 local success, error_msg = OC.Files.move(file_node:getPath(), target_path) if success then -- 返回移动后的文件节点 return target_dir_node:getFile(file_name) else return nil, error_msg end end -- 示例用法(需替换为实际路径) -- local source_file = OC.Files.getFile("/user/files/documents/report.pdf") -- local target_dir = OC.Files.getDirectory("/user/files/archives/2024") -- local moved_file, err = file_move(source_file, target_dir) -- if moved_file then -- print("文件移动成功:" .. moved_file:getPath()) -- else -- print("移动失败:" .. err) -- end
关键说明
- 必须使用 Nextcloud 官方提供的
OC.FilesAPI 进行文件操作,不能使用自定义未定义的函数(如你原脚本中的exists、file.name)。 - 传入的参数需为有效的文件节点和目录节点,可通过
OC.Files.getFile()或OC.Files.getDirectory()获取。 - Nextcloud 会自动处理文件名冲突,默认添加后缀(如
report (1).pdf)。
内容的提问来源于stack exchange,提问作者lolita0242
相关产品推荐
相关产品推荐

