Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File Management System for CCTweaked (Updated)
- local wireless = peripheral.find("modem")
- local isWireless = wireless ~= nil
- local notesDir = "/notes"
- -- Create notes directory if it doesn't exist
- if not fs.exists(notesDir) then
- fs.makeDir(notesDir)
- end
- -- Function to list all notes
- local function listNotes()
- print("Available notes:")
- local files = fs.list(notesDir)
- for i, file in ipairs(files) do
- print(i .. ". " .. file)
- end
- end
- -- Function to create a new note
- local function createNote()
- print("Enter the filename:")
- local filename = read()
- local path = fs.combine(notesDir, filename)
- if fs.exists(path) then
- print("File already exists!")
- return
- end
- print("Enter your note (press Enter twice to finish):")
- local lines = {}
- while true do
- local line = read()
- if line == "" then break end
- table.insert(lines, line)
- end
- local file = fs.open(path, "w")
- file.write(table.concat(lines, "\n"))
- file.close()
- print("Note created successfully!")
- end
- -- Function to view a note
- local function viewNote()
- listNotes()
- print("Enter the number of the note to view:")
- local choice = tonumber(read())
- local files = fs.list(notesDir)
- if choice and files[choice] then
- local path = fs.combine(notesDir, files[choice])
- local file = fs.open(path, "r")
- print(file.readAll())
- file.close()
- else
- print("Invalid choice!")
- end
- end
- -- Function to edit a note
- local function editNote()
- listNotes()
- print("Enter the number of the note to edit:")
- local choice = tonumber(read())
- local files = fs.list(notesDir)
- if choice and files[choice] then
- local path = fs.combine(notesDir, files[choice])
- local file = fs.open(path, "r")
- local content = file.readAll()
- file.close()
- print("Current content:")
- print(content)
- print("Enter new content (press Enter twice to finish):")
- local lines = {}
- while true do
- local line = read()
- if line == "" then break end
- table.insert(lines, line)
- end
- file = fs.open(path, "w")
- file.write(table.concat(lines, "\n"))
- file.close()
- print("Note updated successfully!")
- else
- print("Invalid choice!")
- end
- end
- -- Function to receive a single file
- local function receiveFile()
- local event, _, channel, _, message = os.pullEvent("modem_message")
- if message.action == "file" then
- local path = fs.combine(notesDir, message.name)
- local f = fs.open(path, "w")
- f.write(message.content)
- f.close()
- print("Received: " .. message.name)
- end
- end
- -- Function to sync notes
- local function syncNotes()
- if not isWireless then
- print("This computer doesn't have a wireless modem!")
- return
- end
- print("1. Send notes")
- print("2. Receive notes")
- print("3. Idle mode (receive updates)")
- local choice = tonumber(read())
- if choice == 1 then
- wireless.transmit(1, 1, {action="sync", data=fs.list(notesDir)})
- print("Sending notes...")
- for _, file in ipairs(fs.list(notesDir)) do
- local path = fs.combine(notesDir, file)
- local f = fs.open(path, "r")
- local content = f.readAll()
- f.close()
- wireless.transmit(1, 1, {action="file", name=file, content=content})
- end
- wireless.transmit(1, 1, {action="done"})
- print("Notes sent successfully!")
- elseif choice == 2 then
- print("Waiting to receive notes...")
- wireless.open(1)
- while true do
- local event, _, channel, _, message = os.pullEvent("modem_message")
- if message.action == "sync" then
- for _, file in ipairs(message.data) do
- local path = fs.combine(notesDir, file)
- if not fs.exists(path) then
- print("Receiving: " .. file)
- local f = fs.open(path, "w")
- f.close()
- end
- end
- elseif message.action == "file" then
- receiveFile()
- elseif message.action == "done" then
- break
- end
- end
- wireless.close(1)
- print("Notes received successfully!")
- elseif choice == 3 then
- print("Entering idle mode. Press any key to exit.")
- wireless.open(1)
- parallel.waitForAny(
- function()
- while true do
- receiveFile()
- end
- end,
- function()
- os.pullEvent("key")
- end
- )
- wireless.close(1)
- print("Exited idle mode.")
- else
- print("Invalid choice!")
- end
- end
- -- Main menu
- while true do
- print("\nFile Management System")
- print("1. List notes")
- print("2. Create note")
- print("3. View note")
- print("4. Edit note")
- print("5. Sync notes")
- print("6. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- listNotes()
- elseif choice == 2 then
- createNote()
- elseif choice == 3 then
- viewNote()
- elseif choice == 4 then
- editNote()
- elseif choice == 5 then
- syncNotes()
- elseif choice == 6 then
- break
- else
- print("Invalid choice!")
- end
- end
- print("Thank you for using the File Management System!")
Advertisement
Add Comment
Please, Sign In to add comment