Re: Command to get a notebook's directory?
- To: mathgroup at smc.vnet.net
- Subject: [mg22945] Re: Command to get a notebook's directory?
- From: Dan Warren <drdanw at my-deja.com>
- Date: Fri, 7 Apr 2000 02:54:29 -0400 (EDT)
- References: <8chbfk$9dm@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I ran into the same problem. The reason this seemingly simple problem is difficult to resolve is the directory for the notebook is a front end property; the kernel knows nothing about it. With Allan Hayes' help at last year's Mathematica in the Mountains, I wrote the following front end function: ------ snip -------------- ThisNotebookDirectory::usage = "ThisNotebookDirectory[] returns the directory of the file containing the current notebook. If the file has not been saved, the current working directory is returned.\nIntended usage is SetDirectory[ThisNotebookDirectory[]]." nbdir::"nofile" = "This notebook has not been saved."; ThisNotebookDirectory[] := Block[{info = NotebookInformation[InputNotebook[]]}, If[FreeQ[info, "FileName", {2}], Message[nbdir::nofile]; Directory[], ToFileName[First["FileName" /. info]]]]; --------------- snip ---------------- I put this in my init.m and put the line SetDirectory[ThisNotebookDirectory[]]; at the top of each notebook that needs local file access. This was my first front end function; there's probably room for improvement. In article <8chbfk$9dm at smc.vnet.net>, "Jordan Rosenthal" <jr at ece.gatech.edu> wrote: > Hi all, > > This seems like a pretty simple question, but I am having the hardest time > finding the answer. Let's say I save a notebook in a directory called > "c:\myfiles\mynotebooks". Is there a command I can use from within this > notebook to determine this directory? > > The purpose is to have an Export command that is able to export data to the > same directory in which the notebook is saved. If I move the notebook, I > don't want to have to manually change the Export command. > > The Directory[] command gives the working directory, which is not > necessarily the same as the notebook's directory. > > Thanks, > > Jordan > > Sent via Deja.com http://www.deja.com/ Before you buy.