Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to scan directories and retrieve NBT files with their paths and sizes
- local function scanFiles(directory)
- local nbtFiles = {}
- local function scanDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- scanDirectory(path) -- Recursively scan subdirectories
- elseif file:match("%.nbt$") then
- local size = fs.getSize(path)
- table.insert(nbtFiles, { path = path, size = size })
- end
- end
- end
- scanDirectory(directory)
- return nbtFiles
- end
- -- Function to display a paginated menu
- local function showMenu(files, currentPage, pageSize)
- local totalPages = math.ceil(#files / pageSize)
- currentPage = math.max(1, math.min(currentPage, totalPages))
- term.clear()
- term.setCursorPos(1, 1)
- print("NBT File Explorer")
- print("-----------------")
- local startIdx = (currentPage - 1) * pageSize + 1
- local endIdx = math.min(startIdx + pageSize - 1, #files)
- for i = startIdx, endIdx do
- local file = files[i]
- print(i .. ". " .. file.path)
- print(" Size: " .. file.size .. " bytes")
- end
- print("-----------------")
- print("Page " .. currentPage .. "/" .. totalPages)
- print("Press 'p' to go to the previous page")
- print("Press 'n' to go to the next page")
- end
- -- Function to open an NBT file
- local function openFile(file)
- if fs.exists(file) and not fs.isDir(file) then
- shell.run("nbt", "edit", file)
- else
- print("The file does not exist or is a directory.")
- end
- end
- -- Main program
- local files = scanFiles("/")
- local currentPage = 1
- local pageSize = 5
- local running = true
- while running do
- showMenu(files, currentPage, pageSize)
- print("0. Exit")
- write("Select a file to open (file number or page): ")
- local input = read()
- if input == "0" then
- running = false
- elseif tonumber(input) then
- local fileIdx = tonumber(input)
- if fileIdx >= 1 and fileIdx <= #files then
- openFile(files[fileIdx].path)
- write("Press any key to continue...")
- read()
- else
- print("Invalid file index.")
- sleep(2)
- end
- elseif input == "p" or input == "P" then
- currentPage = currentPage - 1
- elseif input == "n" or input == "N" then
- currentPage = currentPage + 1
- else
- print("Invalid input. Please try again.")
- sleep(2)
- end
- -- Adjust current page to wrap around if exceeded the total pages
- if currentPage > math.ceil(#files / pageSize) then
- currentPage = 1
- elseif currentPage < 1 then
- currentPage = math.ceil(#files / pageSize)
- end
- end
Advertisement
Comments
-
- This script defines several functions for scanning directories and retrieving NBT files with their paths and sizes, displaying a paginated menu, and opening an NBT file. The main program uses these functions to create a file explorer-like interface.
- Here's a concise summary of what the script does:
- 1. The `scanFiles` function takes a directory path as input and recursively scans it for NBT files. It returns a table containing the paths and sizes of the NBT files found.
- 2. The `showMenu` function takes a table of files, the current page number, and the page size as input. It displays a paginated menu showing the files' paths and sizes.
- 3. The `openFile` function takes a file path as input and opens the file using the "nbt edit" command if it exists and is not a directory.
- 4. The main program starts by scanning the root directory ("/") for NBT files and storing them in the `files` table. It sets the initial page number and page size.
- 5. Inside the main loop, it shows the menu, prompts the user for input, and performs the corresponding action based on the input. If the input is "0", the program exits. If the input is a file number, it opens the corresponding file. If the input is "p" or "n", it navigates to the previous or next page, respectively. If the input is invalid, it displays an error message.
- 6. After each action, the current page number is adjusted to wrap around if it exceeds the total number of pages.
- That's a summary of the script's functionality. Let me know if you have any further questions!
Add Comment
Please, Sign In to add comment