# How to get the current directory name in ConTeXt?

Assume the file, source.tex is placed in /home/user/Birds/ and the user types context source.tex to compile the file. Is there a way for ConTeXt to retrieve the name of the folder that the file is located in (in this example, "Birds"), for use as a variable? This might be used, for example, to give the document a title, e.g. \title{\directoryname}.

• This will only be used on a Linux system and needn't be compatible with other systems.
• Directory names with spaces might appear, e.g. /home/user/Tropical\ Fish/.
-

\starttext
\cldcontext {lfs.currentdir ()}
\stoptext

-

In ConTeXt:

\starttext

\ctxlua{context(os.resultof("pwd"))}

\stoptext

-
I'm assuming this is specific to the LuaTeX engine? –  Sean Allred Feb 23 at 18:49
The question is tagged "context-mkiv," so yes, this is luatex. –  Thomas Feb 23 at 21:05
Ah; I'm not terribly familiar with either ConTeXt or LuaTeX, but I understood ConTeXt to be just a format—didn't know MkIV was LuaTeX-specific :) –  Sean Allred Feb 23 at 23:15
@Sean Allred: If you’re curious about Context’s Lua integration, I can recommend mk.pdf, hybrid.pdf and the lists status-files.pdf as well as status-lua.pdf. –  phg Feb 24 at 18:27

The \SetMacroToShellOutput defined below will set the given csname to the ouptut of the shell command that is provided to it. So

\SetMacroToShellOutput{\DirectoryNameWithPath}{pwd}


will set \DirectoryNameWithPath to the full Unix path of the current file name. You can use any Unix comamnds other than pwd and get the desired result. For instance

    \SetMacroToShellOutput{\FileNameWithoutPath}{basename pwd}


will set \FileNameWithoutPath to the current file name (without the path).

## Code:

\documentclass{article}

\makeatletter
\newcommand\SetMacroToShellOutput[2]{%
\begingroup\endlinechar=\m@ne\everyeof{\noexpand}%
\edef\TempResult{\endgroup
\expandafter\def\noexpand#1{%
\@@input|"#2" }}%
\TempResult}
\makeatother

\begin{document}
\SetMacroToShellOutput{\DirectoryNameWithPath}{pwd}%
DirectoryNameWithPath = \DirectoryNameWithPath
\end{document}

-
Or like in Context, Latex users can just use Luatex: \directlua {tex.sprint (lfs.currentdir())}. –  phg Feb 23 at 10:06