How to fetch a directory/object path and to check it

Hello DevForum!

I have recently been creating a plugin that acts like a console for roblox studio.
Example image below if your confused


I am working on the good o’ll cd command, or change directory. I want the console to be able to change its ‘location’, for when I add removing (rm) and listing (ls), etc.

What I am stumped on how to do is go from the string, args[2] to finding if it’s an actual path location (Like game.Workspace or game.StarterGui.SomeGuiName), and after that to edit the name of a certain text label to change accordingly to the new location.

Here is my code currently :

Module Script located in a commands folder

--|| VARS ||--
local command = {}

--|| MAIN ||--
command.Execute = function(args, consoleUI)
	local function newLine()
		local textPrmpt = consoleUI.Assets.TextPrompter:Clone()

		consoleUI.Assets.TextPrompter.TextMain.TextEditable = false
		consoleUI.Assets.TextPrompter.Name = "OldPrompt"

		textPrmpt.Parent =  consoleUI.Assets
		textPrmpt.Name = "TextPrompter"
		textPrmpt.TextMain.Text = ""
		textPrmpt.TextMain.PlaceholderText = " "
	end
	
	if args[2] == nil  then 
		local newResponse = script.Parent.Assets:WaitForChild("Response"):Clone()

		if consoleUI.Assets:FindFirstChild("Response") then
			consoleUI.Assets.Response.Name = "OldResponse"
		end

		newResponse.Parent = consoleUI.Assets

		newResponse.TextColor3 = Color3.new(0.929992, 0.718502, 0.698558)

		newResponse.Name = "Response"
		newResponse.Text = "ERR: please reference a directory/object"

		newLine()
		return
	end
	
    -- Here is where I can use args[2] to figure out if the directory/object exists and how to fetch the name of the directory/object
		
	newLine()
end

return command

If you have any ideas on how to achieve this, they would be greatly appreciated!

Thanks a lot in advance :smiley:!

Well typically with cd, there is the “current” directory stored somewhere…so I would do something like
Here’s my fun crack at it (just pseudo code don’t expect it to run)

local currentDir = game
local function print_directory() --kind of like 'ls'
    for _,v in pairs(currentDir:GetChildren()) do
        print(v)
    end
end
local function changeDirectory(input)
   if input == ".." then
      if currentDir == game then
          warn("Cannot go above Game root")
          return
      elseif currentDir.Parent then
           currentDir = currentDir.Parent
      end
   else
       local foundChildDir = nil
       for _,v in pairs(currentDir:GetChildren()) do
          if string.match(v.Name, input) then
              foundChildDir = v
              break
              --maybe you could add some logic here to 
              --find the "best" string match or something like that
          end
       end
       if foundChildDir and foundChildDir:GetChildren() > 0 then --assuming you only want to 'cd' into things that are like 'directories'
           currentDir = foundChildDir
       else
           warn("No 'directory' found matching that string")
       end
   end

   print_directory()
end

and if you wanted to allow cd PlayerGui/Main/Something you would just have to parse the input variable, delimit by the character you want (like a / or maybe a normal . since that’s how Roblox kinda works), and then do a recurve call to check the path exists and setting currentDir to the last item

1 Like

Cool take at it! I see a few problems with it (of course because it is pseudo code) like input being taken and a few other small tad bits here and there.

I’ll mark it as a solution tomorrow once I test it out and heavily edit it while using some of the systems in this code!

And yes, I’ll be keeping it as PlayerGui.Main but I might change it to PlayerGui/Main, ¯_(ツ)_/¯