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

[ Question ] How to get current Dir?

Started by AnthonyD98™, 07 February 2013 - 06:45 PM
AnthonyD98™ #1
Posted 07 February 2013 - 07:45 PM
How do you get your current dir?

What I'm trying to use is this:

cDir = shell.dir()
fs.makeDir(cDir,"sel")
-- What it should do is get the current directory and then use it in the fs.makeDir
--But it returns an error:
--Line 2 - Expected String

Thanks, AnthonyD98
theoriginalbit #2
Posted 07 February 2013 - 07:48 PM
fs.makeDir(cDir,"sel")
should be
fs.makeDir(cDir.."sel")

EDIT: However there is a problem when using shell.dir() in certain cases.

if you wish to get the directory of the current running program then it wont always work. For example


CraftOS
> someProgram       <-- run the program
                                 <-- base path "/"
> cd somePath        <-- change the active directory
somePath> /someProgram      <-- run it from this directory
somePath                <-- note that its not the programs dir, but the one we are in
somePath>
so how do we fix this? easy

local function getRunningPath()
  local runningProgram = shell.getRunningProgram()
  local programName = fs.getName(runningProgram)
  return runningProgram:sub( 1, #runningProgram - #programName )
end
this gets the running program, which is the full path
somePath/someProgram
then it gets the programs name
someProgram
and returns the substring from the full path starting at index 1 and ending at the full path's end minus the length of the programs name
Edited on 07 February 2013 - 06:56 PM
AnthonyD98™ #3
Posted 07 February 2013 - 07:50 PM
fs.makeDir(cDir,"sel")
should be
fs.makeDir(cDir.."sel")

EDIT: However

That worked but I also had to add a / before the sel

Thanks for your help :)/>

EDIT: Just saw your edit
Edited on 07 February 2013 - 06:51 PM
AnthonyD98™ #4
Posted 07 February 2013 - 07:56 PM
So If I wanted my program to automatically detect what dir it was running in and to apply those changes to the fs functions it does.
so fs.makeDir(".config")
becomes fs.makeDir(cDir.."/.config")

What if I was in root
would the / cause an error?

EDIT: Viewing your edit now . . .
Edited on 07 February 2013 - 06:57 PM
theoriginalbit #5
Posted 07 February 2013 - 07:57 PM
So If I wanted my program to automatically detect what dir it was running in and to apply those changes to the fs functions it does.
so fs.makeDir(".config")
becomes fs.makeDir(cDir.."/.config")

What if I was in root
would the / cause an error?
see the finished edit ;)/>