Classic for-loop in a windows batch file

Question: How can a classic for loop

for (int i=0;i<10;++i) {
// do stuff

be realized in a windows batch file?


echo off
SET /a i=0

IF %i%==10 GOTO END
echo This is iteration %i%.
SET /a i=%i%+1

echo That’s it!

The answer uses goto and the /A option of the set command. Without that option, SET interprets the right side of the equals sign as a string and i would contain ‘i+1’, then ‘i+1+1’ and so on. To make SET interpret the right side as an arithmetic operation, the option /A is required. Enter SET /? to get a detailed explanation of the SET command’s capabilities.

First version of VBA library released

I just put up a first little version of a VBA library, which is a collection of function for doing stuff in Excel VBA. There are a few functions for ranges, strings and VBA modules. Currently (2007-12) I am not doing any larger VBA project. Once I work on something that has more than a few lines of code however, this library will certainly grow.

You can find the whole thing here.

Dynamic Dialogs in Excel VBA

The problem:
You want to have a User Form (a VBA-Dialog) which

  • controls (buttons, text fields etc.) are created at runtime based on information that is read from e.g. a table
  • event handling of those controls is created at runtime.
  • For a solution of these problems check the following file and/or read on.
    Example Code for Dynamic Dialogs in Excel VBA

    Consider the following example: There is a table which contains products of some type. Each product has some properties like description, prize etc. This table is created by some database queries. Hence, you do not know how much entries the table will have.
    You want to provide the user of your Excel application with an easy way to select those products and insert them in another table (e.g. an invoice). If a user wants to insert a product into the invoice, he clicks a button and you would like to present him a User Form that has one button for each product. If he clicks on one button, the concerning product is inserted into the invoice table with its description, price etc.

    Dynamic Dialogs in Excel at Work

    Continue reading

    Making function declarations talk: auto_ptr and memory management in C++

    Here’s just something I wrote some time ago and rotted on my harddisk. It might be helpful for people new to C++.

    In all but the smallest projects, code is much more often read than (re)written. This means, that a large amount of the development time goes into reading code, which therefore should be made as easy as possible. It also means, that almost everything you can do, to make your code easier to be read should be done because the additional time you spent in writing the code will more than pay off in the future. We all know (and perhaps hate) some ways, to make code easier to read: use comments, use long variable names, give functions a clear name that corresponds to their purpose etc.

    These rules can be applied in almost every programming language. But if you and your fellow developers really know the programming language, you can do more. You can express certain expectations and warranties by using features of the language and by this, express yourself more clearly (special bonus: you may even omit a few comments, because your code speaks for itself). Additionally, sometimes the compiler and the runtime environment help you to enforce certain requirements on how your code is to be used (see Design by Contract).

    Continue reading

    Calling one Batch File from another (cascading batch files)

    When you use batch files in windows xp, you may want to call a set of batch files from another batch file. Let’s say your file is named foo.bat and looks like this:

    echo off
    echo "Starting foo.bat"
    echo "Finished foo.bat"

    You will never see Finished foo.bat, since the batch processing will stop after the execution foo1.bat.

    To fix this, you need to call the command line processor for each batch file:

    echo off
    echo "Starting foo.bat"
    cmd /C subFoo1.bat
    cmd /C subFoo2.bat
    echo "Finished foo.bat"

    Or you can use the call-command:

    echo off
    echo "Starting foo.bat"
    call subFoo1.bat
    call subFoo2.bat
    echo "Finished foo.bat"