# Macro to change the extension of filename

I would like to have a macro to change the extension of a file name. For example, my macro needs to take an argument like Figures/Fig.xxx and return me Figures/Fig0.yyy.

I found a solution in Extract filename from path. It is as follows:

\makeatletter
\DeclareRobustCommand{\changefilename}[1]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#1}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
\texttt{\filename@area\filename@base0.yyy}%
\endgroup
}
\makeatother

But, when I use the output of this macro in \includegraphics{}, I get an error.

• You can't put your macro \changefilename inside \includegraphics because it has typesetting commands, thus it doesn't expand to “plain text”. Try this definition: \DeclareRobustCommand{\changefilename}[2][\relax]{% \begingroup \def\textendash{-}% \filename@parse{#2}% \edef\filename@base{\detokenize\expandafter{\filename@base}}% \ifx\relax#1% \texttt{\filename@area\filename@base0.yyy}% \else \xdef#1{\filename@area\filename@base0.yyy}% \fi \endgroup } then you can do \changefilename[\foo]{Figures/Fig.xxx} \includegraphics{\foo}. – Phelype Oleinik Jun 24 '20 at 12:20
• @PhelypeOleinik The expl3 parser would work there ... – Joseph Wright Jun 24 '20 at 12:25
• @PhelypeOleinik Your suggestion is good, can you make it as answer? It will help others... – MadyYuvi Jun 24 '20 at 14:17