Jump to content




[ Question ] How to get current Dir?


  • You cannot reply to this topic
4 replies to this topic

#1 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 07 February 2013 - 07:56 PM.


#3 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 07 February 2013 - 07:50 PM

View PostTheOriginalBIT, on 07 February 2013 - 07:48 PM, said:

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 by AnthonyD98, 07 February 2013 - 07:51 PM.


#4 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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 by AnthonyD98, 07 February 2013 - 07:57 PM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2013 - 07:57 PM

View PostAnthonyD98, on 07 February 2013 - 07:56 PM, said:

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 ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users