need help on for loop in batch file programming

shivam99aa

Disciple
i am learning batch file programming nowadays. i went over loopin in it.i see a code snippet in it which is FOR /D %V IN (*.*) DO DIR/S "%V" my problem is that i am not able to understand how this code works basically what is the role of variable "v" over here how is it working here. plz help me out.
 
1. The first %V is where the variable gets initialised everytime the loop runs.
2. The second "%V" is part of the command which is executed for every loop cycle. The initialised variable gets replaced as a string in the command.
3. The /D tells the for loop to used only directories from the (*.*) set.

Finally, what the command literally does is as follows.
For every directory %V(variable initialisation) in the current directory( specified by *.*) run the command of dir /S directory name( specified by "%V")

dir /S is executed for all directories in the current directory from where the command is executed.
basic help provided by "for /?" command
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory
names instead of file names.
 
Back
Top