2 minute read

I recently created a reorder python script to rename, reorder, add, and remove files/directories in a directory with a numerical naming scheme.

A common file naming practice is to prepend a number to the filename so that the files appear in the order specified by the number. However, say you want to add, delete, or reorder these files, you would have to go through and update all the filenames which have numbers changed from the operation. Although renaming the files like this is not hard, it’s annoying to do when there are multiple files which need to be renamed. I ran into this issue recently while writing my dissertation; each chapter is broken into its own .tex file stored in the chapters/ directory.

chapters
├── 01-introduction.tex
├── 02-related-work.tex
├── 03-previous-work.tex
├── 04-goals.tex
├── 05-tools.tex
├── 06-danna2-work.tex
├── 07-snacc.tex
├── 08-challenges.tex
├── 09-applications-performance.tex
├── 10-accomplishments.tex
├── 11-future.tex
└── 12-conclusion.tex

In order to insert a new chapter or delete a chapter, all the files which come after that chapter will have to be renamed. Further, more in the main .tex file I include each of the chapters with an include statement.

\include{chapters/01-introduction}
\include{chapters/02-related-work}
\include{chapters/03-previous-work}
\include{chapters/04-goals}
\include{chapters/05-tools}
\include{chapters/06-danna2-work}
\include{chapters/07-snacc}
\include{chapters/08-challenges}
\include{chapters/09-applications-performance}
\include{chapters/10-accomplishments}
\include{chapters/11-future}
\include{chapters/12-conclusion}

This means I have to renumber the files in the directory and the files in the include statements. In order to make this operation easier, I created a reorder python script to easily rename, add, delete, and reorder the *.tex files in the chapters directory. This script opens up a text buffer, and you make the desired changes to the buffer; then the script will make the changes to the directory and update the file numbers.

Additionally, I modified my makefile for the \(\LaTeX\) project to include a target to build a chapters.tex file with the proper include statements for the chapters found in the chapters folder. Now in main.tex I only need to \input{chapters.tex}.

Now if I want to add a new chapter between previous-work and goals, as well as rename “previous work” to “prior work”, all I need to do is run reorder chapters. I am then presented with the buffer

01-introduction.tex
02-related-work.tex
03-previous-work.tex
04-goals.tex
05-tools.tex
06-danna2-work.tex
07-snacc.tex
08-challenges.tex
09-applications-performance.tex
10-accomplishments.tex
11-future.tex
12-conclusion.tex

I then change the buffer to

01-introduction.tex
02-related-work.tex
03-prior-work.tex
new-chapter
04-goals.tex
05-tools.tex
06-danna2-work.tex
07-snacc.tex
08-challenges.tex
09-applications-performance.tex
10-accomplishments.tex
11-future.tex
12-conclusion.tex

Then I can rebuild the \(\LaTeX\) document and the changes to the chapters will be included with a newly generated chapters.tex.

Much appreciation to Jonathan Ambrose who helped come up with the idea for the script, motivated me to write it, and added several additional features to the script.

Leave a comment