Batch script loop
Batch script loop
Batch script loop
Re: Batch script loop
`for /l` is your friend:
```
for /l %x in (1, 1, 100) do echo %x
```
Starts at 1, increments by one, and finishes at 100.
**WARNING:** Use `%%` instead of `%`, if it's in a batch file, like:
```
for /l %%x in (1, 1, 100) do echo %%x
```
(which is one of the things I really really hate about windows scripting.)
If you have multiple commands for each iteration of the loop, do this:
```
for /l %x in (1, 1, 100) do (
echo %x
copy %x.txt z:\whatever\etc
)
```
or in a batch file:
```
for /l %%x in (1, 1, 100) do (
echo %%x
copy %%x.txt z:\whatever\etc
)
```
Key:
- `/l` denotes that the `for` command will operate in a numerical fashion, rather than operating on a set of files
- `%x` is the loops variable and will be `(starting value, increment of value, end condition[inclusive] )`
```
for /l %x in (1, 1, 100) do echo %x
```
Starts at 1, increments by one, and finishes at 100.
**WARNING:** Use `%%` instead of `%`, if it's in a batch file, like:
```
for /l %%x in (1, 1, 100) do echo %%x
```
(which is one of the things I really really hate about windows scripting.)
If you have multiple commands for each iteration of the loop, do this:
```
for /l %x in (1, 1, 100) do (
echo %x
copy %x.txt z:\whatever\etc
)
```
or in a batch file:
```
for /l %%x in (1, 1, 100) do (
echo %%x
copy %%x.txt z:\whatever\etc
)
```
Key:
- `/l` denotes that the `for` command will operate in a numerical fashion, rather than operating on a set of files
- `%x` is the loops variable and will be `(starting value, increment of value, end condition[inclusive] )`