I do not know what is meant by 'factorise' here1.
Strategies:
- Use features of Forest 2.1, published 8 years ago, rather than relying only on those available in older versions.
- Use standard TikZ methods to simplify the code and reduce duplication e.g. styles.
- Optionally, use handlers to make the code more succinct.
(1) greatly simplifies the code. (2) greatly reduces duplication. (3) has costs as well as benefits, but an implementation is now included below.
\documentclass[border=10pt,tikz]{standalone} \usepackage[edges]{forest} \usepackage{fontawesome5} \definecolor{foldercolor}{RGB}{124,166,198} \definecolor{filecolor}{RGB}{88,88,88} % ateb: https://tex.stackexchange.com/a/755230/ % cwestiwn projetmbc: https://tex.stackexchange.com/q/755225/ % but the original origin isn't identified - looks like Medina's? \forestset{ fa dir tree/.style={ for tree={ font = \sffamily, folder, grow' = 0, inner ysep = 1pt, inner xsep = 1.75pt, }, }, fa/.style 2 args={ before typesetting nodes={ content/.process={Ow{content}{ \textcolor{#1}{#2}\ ##1 }% }, }, }, open/.style={ fa={foldercolor}{\faFolderOpen}, }, closed/.style={ fa={foldercolor}{\faFolder}, }, file/.style={ fa={filecolor}{\faFile}, }, codefile/.style={ fa={filecolor}{\faFile}, }, pdffile/.style={ fa={red!70}{\faFilePdf}, }, textfile/.style={ fa={filecolor}{\faFile*}, }, imagefile/.style={ fa={blue!70}{\faFileImage}, }, archivefile/.style={ fa={orange!80}{\faFileArchive}, }, } \begin{document} \begin{forest} fa dir tree [system, open [config, closed [settings.conf, textfile] [database.yml, codefile] ] [doc, open [manual.pdf, pdffile] [README.md, textfile] [images, closed [logo.png, imagefile] [banner.jpg, imagefile] ] ] [lib, closed [utils.py, codefile] [helpers.js, codefile] [archive.zip, archivefile] ] [test, open [test\_unit.py, codefile] [test\_integration.py, codefile] [fixtures, closed [data.json, file] ] ] ] \end{forest} \begin{forest} fa dir tree [system, open [config, closed] [Dog, closed] [lib, closed] [test, closed] ] \end{forest} \end{document}

Edit
I would not recommend using a loop here for reasons explained in the comments.
If anything, I would use a handler, as mentioned above.
The reason I don't necessarily recommend this is that you do not really gain much in this case in terms of concision or flexibility. The number of file/directory types is small and the above definitions are very short. Moreover, they are very clear: anybody who knows only a little pgf/tikz will see immediately how to define an additional style, if required.
I like custom handlers, but they obviously are that much less efficient2, and they can make code more obscure. It's not clear to me that the code above is better rewritten this way:
\documentclass[border=10pt,tikz]{standalone} \usepackage[edges]{forest} \usepackage{fontawesome5} \definecolor{foldercolor}{RGB}{124,166,198} \definecolor{filecolor}{RGB}{88,88,88} % ateb: https://tex.stackexchange.com/a/755230/ % cwestiwn projetmbc: https://tex.stackexchange.com/q/755225/ % but the original origin isn't identified - looks like Medina's? \forestset{ fa dir tree/.style={ for tree={ font = \sffamily, folder, grow' = 0, inner ysep = 1pt, inner xsep = 1.75pt, }, }, fa/.style 2 args={ before typesetting nodes={ content/.process={Ow{content}{ \textcolor{#1}{#2}\ ##1 }% }, }, }, /handlers/.fa/.code 2 args={% \edef\projetmbctemptpath{\pgfkeyscurrentpath}% \pgfkeys{% \pgfkeyscurrentpath/.style={% fa={#1}{#2}, }, }% }, open/.fa={foldercolor}{\faFolderOpen}, closed/.fa={foldercolor}{\faFolder}, file/.fa={filecolor}{\faFile}, codefile/.fa={filecolor}{\faFile}, pdffile/.fa={red!70}{\faFilePdf}, textfile/.fa={filecolor}{\faFile*}, imagefile/.fa={blue!70}{\faFileImage}, archivefile/.fa={orange!80}{\faFileArchive}, } \begin{document} \begin{forest} fa dir tree [system, open [config, closed [settings.conf, textfile] [database.yml, codefile] ] [doc, open [manual.pdf, pdffile] [README.md, textfile] [images, closed [logo.png, imagefile] [banner.jpg, imagefile] ] ] [lib, closed [utils.py, codefile] [helpers.js, codefile] [archive.zip, archivefile] ] [test, open [test\_unit.py, codefile] [test\_integration.py, codefile] [fixtures, closed [data.json, file] ] ] ] \end{forest} \begin{forest} fa dir tree [system, open [config, closed] [Dog, closed] [lib, closed] [test, closed] ] \end{forest} \end{document}
This produces exactly the same output and the style definitions are more concise, but it is also rather less obvious what they do. Compare e.g.
/handlers/.fa/.code 2 args={% \edef\projetmbctemptpath{\pgfkeyscurrentpath}% \pgfkeys{% \pgfkeyscurrentpath/.style={% fa={#1}{#2}, }, }% }, open/.fa={foldercolor}{\faFolderOpen},
with
open/.style={ fa={foldercolor}{\faFolderOpen}, },
In both cases, you'd have to lookup the fa style, but in the first, you also have to figure out what .fa does and that means looking up what \pgfkeyscurrentpath refers to, unless you happen to know already3.
However, if you have dozens of these, just annotate the code with an explanation and use the handler.
1I think this is something you do with numbers - paradigmatically, integers. 2But probably not a priority since we are using forest already. 3In which case, you probably wouldn't be asking this question :-).