# Using a stylesheet to create a footer that prints the directory name of the current file

I am trying to use a stylesheet to create a footer that will use the directory name of the current file. For example, consider the following temp.nb file with stored within the CalculusI directory. That is,

CalculusI/temp.nb

I would like the footer to simply print out CalculusI. I have tried the following to no avail. Any ideas?

 Cell[StyleData["Notebook"],
PageFooters->{{None,
FileNameSplit[NotebookDirectory[]][[-1]], None}, {None,
None, None}},
PrintingOptions->{"FacingPages"->True,
"FirstPageFace"->Right,
"FirstPageFooter"->True,


I can succesfully get the filename as the footer by using the following in the stylesheet replacing FileNameSplit[NotebookDirectory[]][[-1]] in my code above with the following.

CurrentValue["NotebookFileName"]


However, I can't figure out how to grab the directory name (CalculusI) for the file and use it as the footer. Any ideas?

The main problem is that you must use boxes in the PageFooters option setting. A typical boxes expression consists of *Box objects with lists and strings. For example:

ToBoxes[f[x, "y"]]


RowBox[{"f", "[", RowBox[{"x", ",", "\"y\""}], "]"}]

CurrentValue is special, because it gets evaluated by the FrontEnd, and it evaluates to a string. This is why you can use CurrentValue in the option setting. However, adding FileNameSplit means the FrontEnd can't evaluate it, so it is an expression and doesn't get rendered properly.

So, use a Cell object instead. For example, the following returns the desired directory:

Dynamic @ FileNameTake @ CurrentValue["NotebookDirectory"]


The corresponding Cell expression is:

Cell @ BoxData @ ToBoxes @ Dynamic @ FileNameTake @ CurrentValue["NotebookDirectory"]


Cell[BoxData[ DynamicBox[ ToBoxes[FileNameTake[CurrentValue["NotebookDirectory"]], StandardForm]]]]

The following style should do what you want:

Cell[StyleData["Notebook"],
PageFooters->{
{
None,
Cell[
BoxData @ DynamicBox @ ToBoxes @ FileNameTake @ CurrentValue["NotebookDirectory"],
ShowStringCharacters->False
],
None
},
{None, None, None}
}
]


You almost had it!! I overcompplicated this initially in my answer, but just change "-1" to "-2" as in:

NotebookFileName[]


Gives you:

C:\Users\Cody\Desktop\directorymain\subdirectory\targetnb.nb


And so:

FileNameSplit[NotebookFileName[]][[-2]]


Gives you:

subdirectory


As output! Relevant references:

Hope this helps!! Also of value: https://reference.wolfram.com/language/workflow/ConstructAFilePathProgrammatically.html

The workflow pages are some of my more favorite ones to review for strategies.

• I’m sorry. I should have been more clear. My problem isn’t how to get the name of the directory. Instead it is how to maybe it appear as a footer using a Stylesheet. – Michael McCain Apr 18 '19 at 19:56
• @MichaelMcCain you would just replace what you reference in your question, I am on mobile currently and can test it in a bit, but: "I can succesfully get the filename as the footer by using the following in the stylesheet replacing FileNameSplit[NotebookDirectory[]][[-1]] in my code above with the following." So it should work if you replace that with FileNameSplit[NotebookFileName[]][[-2]] --If my understanding is correct. I can edit my answer when I get to a PC – CA Trevillian Apr 18 '19 at 20:39
• Hello. I tried putting this direclty into the stylesheet. For some reason it doesn't like the call. It does work with CurrentValue["NotebookFileName"], but I don't want the file name. I can't seem to find a similair way using CurrentValue to get the directory name. – Michael McCain Apr 20 '19 at 6:43
• @MichaelMcCain and, to be clear, it would print just "temp.nb"? Or "Calculusl/temp.nb"? – CA Trevillian Apr 20 '19 at 6:46
• Yes... using CurrentValue["NotebookFileName"], I get temp.nb as the footer when I print to PDF. – Michael McCain Apr 20 '19 at 6:47