File List in .txt file

with explanation (IT Versione)

Saturday morning. The sky is black, heavy, the same color as my mood when I can’t get to the outdoor work. And it’s raining. Annoying rain. When it rains, something always happens: a drain decides to clog, a well rises more than it should, a gutter leaks right at the most inconvenient spot. And I, having only Saturday for “extra chores” – because Sundayi am a Family man – find myself pacing around the house like a restless bee, staring out the window hoping it will stop, or sticking my head out the door to see if I can go out “even with a little rain.” At some point, resigned, I name the day: “Day of annoying work tasks.” And so, between one thing and another, I reopen an old task. But really old. One of those that has been lying in a corner for months, so boring that even rereading it seems to make it sigh and complain.

Some time ago, a query – offended for reasons I won’t explain here – decided to produce hundreds of files instead of dozens. And it did so for three days straight. Fixing this is possible, actually necessary, but it meant comparing logs and files scattered across folders full of stuff. A job that would take hours, maybe days. And here my method comes into play: when faced with a long and repetitive task, I invest 20% of the time to figure out how to make my computer friend do 80% of the work. It’s a pact we’ve had for years: I think, it grinds.

I usually start from the end: I write down a sentence that describes the final goal. Then I break the work into major steps, and working backward, I build the action plan. A little ritual. And like every ritual worth its salt, the first thing I do is open my LDU. The Little Devil’s Utility: a folder containing thirty years of batch files, Excel sheets, formulas, micro-applications, and other devilish tools. An arsenal of instruments born to solve problems, often created on days like this. It once even had a custom icon, a little red devil… then one day the file got corrupted and goodbye mascot. My folder of forgotten little magic offers me everything except, of course, what I need. So let’s arm ourselves with imagination, a couple of old books, and some chocolates for “the dance of the successful test.”

Now, you who are reading might think:

“But excuse me, isn’t it enough to just do dir > list.txt?”

Yes, of course. Nothing could be easier. But try doing it for 300 folders, each with hundreds of files. The repetition eats away at your soul. So… why not have some fun? Why not let an old friend – the batch file – do the dirty work? My task started just like that: listing the contents of directories. And while I was at it, I decided to do it right, with a simple yet elegant DOS script. Automating small daily tasks is one of the most effective ways to save time and reduce errors, and this is one of those cases where a bit of automation saves your day and puts your mood back on track. In this article, I’ll share the script I created and, most importantly, explain why each command is there. Not just four lines thrown together, but a little journey into the logic of a well-crafted batch file.

The complete script

Line by line analysis

1. @echo off

Disables the display of commands while they are being executed. This helps make the output cleaner and more readable by showing only what we want to communicate to the user. When a batch script runs, by default it shows each command before executing it. This creates a lot of visual “noise.” The line @echo off tells the prompt: “Execute the commands, but do not display them on the screen.”

The at symbol @ is also used to hide that same line, so the script starts clean.

Does the opposite: reactivates the display of commands. It is useful when you want to debug or understand what is happening inside the script.

Example:

From that point on, each command will be displayed before being executed.

It is a comment. It is used to write notes, explanations, or reminders within the script. The command is not executed and does not affect the functionality.

Example:

You can also use :: as a faster alternative, but “rem” is more readable and compatible.

2. setlocal enabledelayedexpansion

Enable delayed expansion, which is necessary when using variables that change inside a loop (for). Without this option, variables like !NAME! or !OUTFILE! would not update correctly.

3. set DIRLIST="mia_dir..." "mia_dir/dir2..."

Here we define the list of folders to analyze. The important points are that:

  • the paths are enclosed in quotation marks (to handle any spaces)
  • they are in the same variable, therefore separated by a space
    This allows the for loop to iterate over each folder independently.

4. echo Generazione report...

A simple message to the user: the script is starting the work.

The “for” cycle

The heart of the script. For each directory contained in DIRLIST, the variable %%D takes the value of the current path.

6. echo Analizzo %%D...

Informative message: useful for understanding which folder is being processed.

7. set "NAME=%%~nD"

Extract only the name of the folder, “without path”. Example:

  • c:\mia_dir\SCAN3492SCAN3492
  • c:\mia_dir\SCAN3492\TransferTransfer

This name will be used to generate the output file name.

8. set "OUTFILE=%%D\Filelist_!NAME!.txt"

Here we build the complete path of the text file to be generated.

Esempio:

c:\mia_dir\SCAN3492\Filelist_SCAN3492.txt
c:\mia_dir\SCAN3492\Transfer\Filelist_Transfer.txt

Therefore, each folder contains its own report, there is no need for a centralized folder, the file is always located next to the data it describes.

9. Il comando "dir"

dir "%%D" /a:-d /t:w > "!OUTFILE!"

Here born the really command:

  • "%%D" → folder to analize
  • /a:-d → Show only files without directory
  • /t:w → sort by last modified date
  • > → redirects the output to a text file

Important note: there is no /s, so the script does NOT go into subfolders because originally this is exactly what I wanted, only the files in the root.

10. echo done.

Final confirm message

11. pause

Keeps the prompt window open until the user presses a key. Useful when running the script by double-clicking instead of through the command prompt console.

In brief, everything I insert as a path inside DIRLIST, enclosed in quotes, is listed in a file named foldername.txt. A few lines of batch code, and the problem is solved elegantly. To make my life easier, I first extracted the list of folders to analyze, so I could quickly “clean them up”: add the quotes, fix the paths, and paste them ready into my script. Once the paths were combined with the batch file, the job took less than a minute. The next step – and also the most interesting one – was to reconstruct a daily log starting from an inexplicable fragmentation of dozens of micro-logs. There too, I found a semi-automatic and reliable method to merge everything, but the final reading was inevitable: logs need to be checked, because just one truncated line can cause important information to be lost. That part was less fun, of course. But still better than being out in the rain.

Thank you for your time

L.C.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *