KerberX
Full Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору MisterMyth Держи функцию. Код: function Folder.GetParent(sourcePath, level) local level = level or 1; if (level <= 0) then return sourcePath; end local function DelimitedStringToTable(sourceString, delimiter) local result = {}; local delimiterLength = string.len(delimiter); if (delimiterLength < 1) then result[#result + 1] = sourceString; return result; end local nPos = string.find(sourceString, delimiter, 1, true); local i = 0; while (nPos ~= nil) do local foundPart = string.sub(sourceString, 1, nPos - 1); result[#result + 1] = foundPart; sourceString = string.sub(sourceString, foundPart:len() + delimiterLength + 1, -1); nPos = string.find(sourceString, delimiter, 1, true); end if (sourceString ~= "") then result[#result + 1] = sourceString; end return result; end local paths = DelimitedStringToTable(sourcePath, "\\"); if (#paths ~= 0 and level < #paths) then return table.concat(paths, "\\", 1, #paths - level); end return ""; end | Использовать так: Код: local s = "Z:\\Soft\\Microsoft Office\\Microsoft Office 2003"; Folder.GetParent(s, 0); --> "Z:\\Soft\\Microsoft Office\\Microsoft Office 2003" Folder.GetParent(s, 1); --> "Z:\\Soft\\Microsoft Office" Folder.GetParent(s, 2); --> "Z:\\Soft" Folder.GetParent(s, 3); --> "Z:" Folder.GetParent(s, 4); --> "" (пустая строка) |
|