Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

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.



1 May 2013

AutoIt - The Scripting Language for Windows

Introduction
AutoIt is a free and excellent scripting language. It's almost a full programming language because you can do so much with it. Primarily it was designed for automating installations but it's grown and grown over the years. The best thing about it is that you write the code in a text editor (Notepad or anything you want) then compile it. Once compiled your resulting program is a standalone exe file! Yes, no DLL files to package with it!


Download and Install
http://www.autoitscript.com/

Along with AutoIt itself (it includes the compiler, samples and help files) I highly recommend you download and install the editor for it, SciTE.


SciTE
This code editor makes writing AutoIt scripts a breeze. It colour codes the commands, click a word and press F1 to see help on the command/function. It even has links on the menus so you can easily create form windows and it pastes the automatically generated code into the text editor for you.


The above is SciTE with an AutoIt script in it. You can see the colour coding, for example, the comments are in green, variables are red, etc.


An Example AutoIt Script
Here's an example of what's possible with AutoIt. It's a little script I wrote to convert text from hex to text and back again. It outputs the results to a text file and opens it in Notepad.

; Hex/Text Converter
; By Michael Gerrard
; 01/11/2011
; AutoIt 3

$title  = "Hex/Text Converter"
$input = ""
$output = ""
$outputFile = @ScriptDir & "\HexText-output.txt"

$input = InputBox($title, "Enter a hex or text:")
If @Error = 1 Then Exit

$iCheck = StringInStr($input, ":", 0, 1, 1)
If $iCheck = 3 Then ;if successful it returns 1, a colon was found and therefore it's a hex string
 _CreateText()
Else
 _CreateHex()
EndIf

; Output the result to a file
$file = FileOpen($outputFile, 2)
FileWriteLine($file, $input)
FileWriteLine($file, $output)
FileClose($file)
Run('notepad "' & $outputFile & '"', @ScriptDir)
Exit




; =========
; Functions
; =========

; Create a Hex string from Text
Func _CreateHex()

  $i = StringSplit($input, "")

  ; Make the hex numbers from each character of the text string
 For $n = 1 to $i[0]

   $hex = StringRight(Hex(Asc($i[$n])), 2)

   ; Add each char together with colons
  If $n = $i[0] Then
   $output = $output & $hex
  Else
   $output = $output & $hex & ":"
  EndIf
 Next

EndFunc


; Create a Text string from Hex
Func _CreateText()

  $i = StringSplit($input, ":")

  ; Take the hex numbers and convert them to text
 For $n = 1 to $i[0]

   $iText = Chr(Dec($i[$n]))

   ; Add each char together with colons
  $output = $output & $iText
 Next

EndFunc



What else can you do?
Read and write to the registry, to ini files easily with dedicated commands, control windows, send keystrokes to programs, interact with window controls, run command line DOS programs, pass variables to DOS programs, etc. AutoIt is really for making scripts to automate a process but you can also write GUI (Graphical User Interface) forms and other windows.


Conclusion
I've used AutoIt countless times for many years. It is powerful yet easy to learn. If you want to automate Windows this is one of the best scripting languages out there.

Recommended: 9.9 / 10



28 Aug 2012

Learn C#

I've started learning C# (pronounced C sharp). It's a modern object orientated programming language. I found a number of good resources:

Download the free Visual C# Express IDE (Integrated Development Environment) software from here:
http://www.microsoft.com/express/

Learn Visual C#:
http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx

'How do I' videos:
http://msdn.microsoft.com/en-us/vstudio/bb798022

For anyone with a Windows Phone, take a look at the Learning Circle app, you can search for it in the Marketplace or click this link:
http://www.windowsphone.com/en-US/apps/5bd14f56-26e7-42ff-81db-e20ffb7f8bf2
When you run this app click the Software Development tile and you'll see many videos about C# and other languages.

The above app and all the resources I've listed here are free. It seems like there's a lot of help available to get started with C#. It's great news because normally when you think of Microsoft you think of having to spend money. In this case they are providing a really good opportunity to get familiar with this interesting technology.