Techie Talks

Saturday, March 26, 2005

Looping command in MS-DOS

The main purpose of this write up is to explain a use of "FOR" in MS-DOS to run a command various times with just a numerical parameter in it changing sequentially.

Suppose there is a folder "fol" that contains sub-folders with names fol01, fol02.... fol99. Let it be that we need to delete only folders fol11, fol12... fol20. To do so, the following DOS command comes handy:

FOR /L %var in (11, 1, 20) DO del fol\fol%var

In general, the syntax for the command is

FOR /L %variable in (start, step, end) DO command
(where start, step and end are integers)

If this command is being used within a batch file, %%variable must be used instead of
%variable. This is because %i (where i is an integer) stands for the ith input parameter to a batch file. Hence, the system would take %variable for an input parameter value and not the loop iterator variable. This is solved by using %%variable.

0 Comments:

Post a Comment

<< Home