MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Finding the Path to/Directory for the current EvaluationNotebook?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43432] Re: [mg43423] Finding the Path to/Directory for the current EvaluationNotebook?
  • From: YAT <Yasvir-Tesiram at omrf.ouhsc.edu>
  • Date: Wed, 17 Sep 2003 07:58:48 -0400 (EDT)
  • References: <200308291116.HAA15069@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
Stuff follows below

On Fri, 29 Aug 2003, AES/newspost wrote:

> I want to put a SetDirectory (?) command at the beginning of a notebook
> that will reset the working directory to the folder containing the
> notebook itself.
>
> Objective is to have the results of any Exports, OpenWrites, etc,
> executed within that notebook automatically be directed to that same
> folder -- even if I close the notebook, rename it in the Finder (in Mac
> jargon), and re-open it.
>
> Or, if I Copy or Move the notebook to a different folder and later open
> it there, Exports or OpenWrites should go to that new, different folder.
>

Hi,
Here something that you may want to put at the beginning of your notebook
and make it an initialisation celly. Alternatively you can make a package
that you can load and then call the function. Hopefully this is platform
independent.

(* you need to load this package *)

<<AuthorTools`

setDirectoryToCurrentFileDirectory[]:=
  Module[{curOpenFiles,pathOfOpenFile,nbName,pathToFileDirectory},

    (* shows current objects *)
    curOpenFiles=Notebooks[];

    (* I assume that the notebook of interest is the first object *)
    pathOfOpenFile=NotebookFilePath[curOpenFiles[[1]]];

    (* this shows the name of the open notebook*)
    nbName=NotebookName[curOpenFiles[[1]]];

    (* make the path into a useful string by dropping the filename from it
*)


    pathToFileDirectory=StringReplace[pathOfOpenFile,nbName->""];

    (* finally set the current working directory *)
    SetDirectory[pathToFileDirectory]

    ]



setDirectoryToCurrentFileDirectory[]



You can write all of the above into an initialisation cell so that it
evaluates everytime you open that notebook.

There are probably many other ways to handle this sort of stuff but this
seems the most direct. There is no error handling or final checking to
see if you are in the correct directory. I tested this on a PC with
Windows XP with two different files and it returned the correct directory.

Cheers
Yas


  • Prev by Date: Re: Re: Re: v5 for Mac
  • Next by Date: Re: discrete frequency distribution
  • Previous by thread: Re: Re: Re: Finding the Path to/Directory for the current EvaluationNotebook?
  • Next by thread: Re: Finding the Path to/Directory for the current EvaluationNotebook?