Batch Files

Creating Monthly Folders


This page was written because on July 1, 2023, Google Analytics introduced G4 which I found overkill for what I want. I decided to try and stop using it and took a look at older web log analyzers such as Analog, AWStats, W3Perl, and Webalizer.

The way that some of these work, I needed to create folders for each year and month. As I have several years of log files, creating them manually while processing them is tedious so I wrote a batch file to create them for me. This page looks at several methods of creating the folders.


Environment Variables

Windows provides variables to provide information about the computer system and its users. They are kown as environment variables and can help when writing batch files.


Loops in batch files can behave very strangely. Sometimes it looks as if they were written correctly but just one value, usually the final in the FOR list, will be evaluated and executed. What is happening is the entire loop is being evaluated, but as a single command which means only the last value is being acted on. This can be remided by making the first line of the batch file

setlocal EnableDelayedExpansion

and ending it with


See the SS64site for explanations of the Setlocal, EnableDelayedExpansion, and Endlocal commands.

mkdir vs md

These are the same command, there is no difference between them, one is an alias of the other. mkdir on Linux has the same functionality as on Windows, but Linux does not recognise the md command.


MKDIR can make folders several layers deep. Suppose you want to make a folder structure such as 2023\2023-nov The command

mkdir documents\2023\2023-nov

will do that. If the folder 2023 already exists then 2023-nov is created in that. If it does not exist then it is created.

If the full path already exists, then a message to that effect will be seen on the command line.

The Script

These scripts assume that the user's Windows default folders have not been moved or renamed.

Suppose we want to make a folder named 2023 in the Documents folder. One method of doing it is via a batch file, I used variables in this as I will be introducing a loop later on to create the month folders:

set basedir=%userprofile%\documents
set year=2023
set yeardir=%basedir%\%year%
mkdir %yeardir%

But suppose new folders for each month under the 2023 folder are needed. In that case, the following will make all the folders:

set basedir=%userprofile%\documents
set year=2023
set yeardir=%basedir%\%diryear%
for %%a in (01 02 03 04 05 06 07 08 09 10 11 12) do (mkdir %yeardir%\%year%-%%a)

Folders created using the above code

The folders created using the above code

Of couse the batch file can be adapted for various formatting, for example:

set set basedir=%userprofile%\documents
set year=2023
set yeardir=%basedir%\%diryear%
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (mkdir %yeardir%\%%a-%year%)

Which results in the following folder structure:

Folders created using the above code

The folders created using the above code

Sources and Resources

EnableDelayedExpansion (SS64)
Endlocal (SS64)
Environment Variables (SS64)
For (SS64)
MD (SS64)
MS-DOS and Windows command line md and mkdir commands (Computer Help)
Setlocal (SS64)