9 Jun 2013

AutoIt - Create Year and Month folders

Year and Month foldersIntroduction
I wanted to create folders from 1998 to 2013. With sub-folders for all the months 01 to 12 in each.

1998
Sub-folders 01 to 12 to represent the months.

1999
Sub-folders 01 to 12 to represent the months.

2000, etc... The image opposite shows what it should look like.

I did not want to create these folders manually in Windows Explorer, it would've taken too long and it's a boring job. Perhaps I'd need to do this again another time, having a script (a program) I could run to do this quickly seemed to be a good idea.

There are a few ways of doing this in an automated way. You could create the series of folders in Excel as a column. Then in column A you could put MD (Make Directory). Then copy/paste that to a text file and you could make a batch file to create the folders for you. Of course this method is not elegant and you can't easily reuse it.

A good alternative is to use AutoIt. You can quickly make a script and modify it later if need be. Here in this article I'll show you how to do this. We will use For...To...Next loops.


AutoIt
It's a scripting language for Windows, it is free and you can download it from here: www.autoitscript.com
Also, there's my previous article introducing AutoIt here.


Code
Here's my code, feel free to copy/paste it.

#cs ----------------------------------------------------------------------------
CreateYYYYMM
Create a folder structure of YYYY\MM (year and month)
By Michael Gerrard, 9/6/2013
#ce ----------------------------------------------------------------------------

; Script Start 

$title   = "Create Year and Month folder structure" 
$home    = @ScriptDir
$startY  = "1998"
$endY    = "2013"
$startM  = "1"
$endM    = "12"

; Loop and create the Year folders
For $y = $startY to $endY Step 1
    
    ; Create Year folder
    DirCreate($home & "\" & $y)
    
    ; For each year folder create a sub-folder for Months
    For $m = $startM to $endM Step 1
        
        ; You need folders that are MM so you need to check if it's just one character
        ; then add a 0 to the front to make months like 01, 02, 03, etc. 
        $length = StringLen($m)
        If $length = 1 Then
            $m = "0" & $m
        EndIf
        
        ; Create Year folder
        DirCreate($home & "\" & $y & "\" & $m)
    Next
Next

MsgBox(0, $title, "Finished")
Exit

NOTE: The code in this article was tested using AutoIt 3.3.6.1.


Explanation
At the top there are comments within a starting tag of #cs and ending with #ce (CS = Comment Start, CE = Comment End). Other comments start with a semi-colon ";".

The script starts with some variables being declared. Variables start with a dollar sign "$". These are the parameters I am feeding into the script - they are the input. The basics of any program are input, process and output, this is a classic example. You can see I've put the range (scope) in the variables at the top. The $startY variable is for the start of the year range for example.

The code really starts at the first For...To...Next loop. First we start by creating the year folders. This means we have to loop from 1998 to 2013.

For $y = $startY to $endY Step 1

The $y is the counter, it's where the results of the loop will go. The $startY to $endY is the equivalent of 1998 to 2013 in this example (see the variables declared at the beginning). Step 1 means as it loops it will increment by one each time. This means that the code within the For and the last Next command will be executed starting at 1998, 1999, 2000, counting up to 2013.

DirCreate($home & "\" & $y)

The DirCreate function makes a folder. The variable $home is from the declared variables at the beginning of the script. I have set it to @ScriptDir - that means the same folder location where the script file is. The slash & sign adds the variable to the \ string and $y variable. The \ is needed as it is a folder delimiter. The $y is the year variable, the counter that is in the For statement. To start this command would create a folder "\1998". Below it we need to create 01 to 12 for the months...

For $m = $startM to $endM Step 1

$m is the counter here, it's the month number. The $startM and $endM are 1 to 12. It'll loop 12 times.


The month folders should be named 01, 02, 03, to 12. They can't be 1, 2, 3, etc because they will not line up properly in Windows Explorer. The script will work with just 1, 2, 3, etc. Therefore we need to add a zero to the front if the number is 1 to 9 (single digit numbers). Therefore I added this code next:

$length = StringLen($m) 
If $length = 1 Then 
     $m = "0" & $m 
EndIf

First we find out how many characters the $m variable is. Is it 1 to 9 or a two digit number? If it's a single digit number then add a zero to the front.

DirCreate($home & "\" & $y & "\" & $m)

The above will create a month folder. It will loop and create the folders 01 to 12.

MsgBox(0, $title, "Finished")

The MsgBox function shows a window on the screen with an OK button. It's just a notification the script has ended, it's not essential but nice.


Conclusion
AutoIt is an excellent way to create a little script very quickly to do any job, especially creating folders like in this example. The best thing is that you can adapt this to your needs. You could even add an interface to prompt the user to enter the years to start and end at. Or maybe add those parameters from the command prompt - it's all possible with AutoIt.