追加檔案副檔名 (file.path.add_extension)
聲明
新路徑, 錯誤資訊 = file.path.add_extension(路徑, 副檔名)
參數
- 路徑
文字型,基礎路徑字串 - 副檔名
文字型,要追加的檔案副檔名,通常不包含前導.
回傳值
- 新路徑
文字型 或 nil,成功時回傳追加副檔名後的路徑字串,無法追加時回傳 nil - 錯誤資訊
文字型,路徑字串無效時回傳"Invalid path",副檔名字串無效時回傳"Invalid extension";副檔名包含/等導致無法追加時,可能只回傳 nil 而不提供錯誤資訊
說明
在路徑末尾追加
.和指定的副檔名;如果原路徑已有副檔名,新副檔名會追加在現有副檔名之後,例如file.txt追加bak後為file.txt.bak
傳入的副檔名可以包含前導.,但此函式不會去掉它;傳入.txt會追加..txt
此函式只生成路徑字串,不會重新命名實際的檔案或目錄
該函式在 20250914 以後的版本方可使用
範例
local new_path = file.path.add_extension("/path/to/file", "txt") -- 回傳 "/path/to/file.txt"
local archive_path = file.path.add_extension("/path/to/file.tar", "gz") -- 回傳 "/path/to/file.tar.gz"
local backup_path = file.path.add_extension("/path/to/file.txt", "bak") -- 回傳 "/path/to/file.txt.bak"
local dotted_path = file.path.add_extension("/path/to/file", ".txt") -- 回傳 "/path/to/file..txt"