This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BigSHinyToys's profile picture

File and Folder information tool size,location,read/write status

Started by BigSHinyToys, 03 September 2012 - 09:28 AM
BigSHinyToys #1
Posted 03 September 2012 - 11:28 AM
This is a little tool that shows all of the vital stats for a file.


Download - ver 0.2 build 2
http://pastebin.com/cDxxAzJu

pastebin get cDxxAzJu FileInfo
Spoiler

--[[
		file infomation tool
		by BigSHinyToys
		vre 0.1 build 1
]]--
local tArgs = {...}
if #tArgs ~= 1 then
	print("usage <fils parth and name>")
	return
end
local function folderSize(Input)
	local nTotel = 0
	local nFilesA = 0
	local NfoldersA = 0
	local tList = fs.list(Input)
	for i = 1,#tList do
		if not fs.isDir(Input..[[]]..tList[i]) then
			nTotel = nTotel + fs.getSize(Input..[[]]..tList[i])
			nFilesA = nFilesA + 1
		else
			local nSize,nFiles3,NfoldersB = folderSize(Input..[[]]..tList[i])
			nTotel = nTotel + nSize
			nFilesA = nFilesA + nFiles3
			NfoldersA = NfoldersA + NfoldersB + 1
		end
	end
	--print(tostring(nTotel).." "..tostring(nFilesA).." "..tostring(NfoldersA))
	return nTotel , nFilesA , NfoldersA
end

local nFolders = 0
local nFiles = 0
local nSize = 0
print("-----Infomation------")
if fs.exists(tArgs[1]) then
	if not fs.isDir(tArgs[1]) then
		print("Type : File")
		print("Name : "..fs.getName(tArgs[1]))
		local sizBytes = fs.getSize(tArgs[1])
		local one,two = math.modf(sizBytes/1024)
		print("Size : "..tostring(one).."."..string.sub(tostring(two),3,4).." KB ("..sizBytes.." bytes )")
		local where = fs.getDrive(tArgs[1])
		if where == "hdd" or where == "rom" then
			print("Location : "..where)
		else
			print("Location : "..where.." disk drive")
		end
		if fs.isReadOnly(tArgs[1]) then
			print("File is read only")
		else
			print([[Read / write enabled]])
		end
	else
		print("Loading Directory")
		local termX,termY = term.getSize()
		local curX,curY = term.getCursorPos()
		local tFiles = fs.list(tArgs[1])
		for i = 1,#tFiles do
			term.setCursorPos(1,curY)
			term.clearLine()
			write(string.rep("=",(i/#tFiles)*(termY)))
			local sProgress = tostring(math.floor((i/#tFiles)*100)).." %"
			term.setCursorPos((termX/2)-(#sProgress/2),curY)
			write(sProgress)
			if fs.isDir(tArgs[1]..[[]]..tFiles[i]) then
				nFolders = nFolders + 1
				local byte,nFiles2,nfolders2 = folderSize(tArgs[1]..[[]]..tFiles[i])
				nSize = nSize + byte
				nFiles = nFiles + nFiles2
				nFolders = nFolders + nfolders2
			else
				nFiles = nFiles + 1
				nSize = nSize + fs.getSize(tArgs[1]..[[]]..tFiles[i])
			end
		end
		term.setCursorPos(1,curY)
		term.clearLine()
		print("Type : folder")
		print("Name : "..fs.getName(tArgs[1]))
		print("Contains "..nFiles.." files, "..nFolders.." folders")
		local one,two = math.modf(nSize/1024)
		print("Size : "..tostring(one).."."..string.sub(tostring(two),3,4).." KB ("..nSize.." bytes )")
		local where = fs.getDrive(tArgs[1])
		if where == "hdd" or where == "rom" then
			print("Location : "..where)
		else
			print("Location : "..where.." disk drive")
		end
		if fs.isReadOnly(tArgs[1]) then
			print("Folder is read only")
		else
			print([[Read / write enabled]])
		end
	end
else
	print("Not file or Folder")
end
print("---------------------")
KaoS #2
Posted 05 September 2012 - 11:22 AM
very cool, but for the directory side why don't you make it cycle through the files and directories within it and add all of their size for a collective size?
BigSHinyToys #3
Posted 05 September 2012 - 02:48 PM
very cool, but for the directory side why don't you make it cycle through the files and directories within it and add all of their size for a collective size?

Updated to include folder data
KaoS #4
Posted 05 September 2012 - 02:50 PM
Much better, and very well written I must say, +1