# Defining command \file#1 and use it with \input command, and counter \thefile#1

I would like to define a command as \file#1 in order to use it with \input{\file#1}, where #1 is a entry given, as well as counter \thefile#1

The MWE is the next code

\documentclass{article}
\usepackage{xstring}
\newcommand{\fileone}{testfile}
\newcommand{\file}[2]{%
\IfStrEqCase{#1}{%
{one}{%
\newcounter{file#1}\setcounter{file#1}{#2}\thefileone
\input{\fileone}
}
}}

\begin{document}
Hello, \file{one}{20}
\commandone
\end{document}


and the file testfile.tex has \commandone definined as \newcommand{\commandone}{This a test in file "\fileone"}.

I would like to replace the word "one" by "#1" in order to use \thefile#1 and \input{\file#1} on definition of \file.

• What's the purpose of defining a new counter? – egreg Nov 24 '15 at 21:53
• I don't understand the whole purpose of this! Code golfing? ;-) – user31729 Nov 24 '15 at 21:58

Defining a new counter on the fly is not always a good idea, but this works:

Generating command sequences from other macros or tokens is done with \csname file#1\endcsname, for example, the same for the counter output \csname the#1\endcsname.

I suggest to use \InputIfFileExists{}{}{} instead of \input{...} only

\documentclass{article}
%\usepackage{xstring}
\newcommand{\fileone}{testfile}

\newcommand{\filethree}{otherfile}

\begin{filecontents}{\filethree.tex}
\newcommand{\commandthree}{This a test command in file "otherfile"}
\end{filecontents}

\newcommand{\file}[2]{%
\newcounter{file#1}\setcounter{file#1}{#2}\csname thefile#1\endcsname%
\InputIfFileExists{\csname file#1\endcsname}{}{}%
}

\begin{document}
Hello, \file{one}{20}
\commandone

Now: \file{three}{1000}

\commandthree
\end{document}