One of the problems is the following line. You're converting the executable's name to an integer, which could be a little troublesome.
Code:
d := integer.Parse(args[0]);
Which ought to be:
Code:
d := integer.Parse(args[1]);
This will cut your loop way down.