> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/prog/windows-scripting/looping.md).

# Looping

## Loop through files

Without any additional parameters you can loop through files as follows

```batch
C:\Program Files\hashcat-6.2.6\rules>for %r in (*.rule) do @echo %r
best64.rule
combinator.rule
d3ad0ne.rule
dive.rule
generated.rule
generated2.rule
<---snip--->
```

Note the `@`-sign before the `echo` command to prevent the explicit printing of the command.

## Loop through files starting at Path

With the `/R` parameter you can read files recursively starting at the specified path

```batch
C:\Program Files\hashcat-6.2.6>for /R rules %r in (*.rule) do @wc.exe -l "%r"
102 C:\Program Files\hashcat-6.2.6\rules\best64.rule
70 C:\Program Files\hashcat-6.2.6\rules\combinator.rule
34158 C:\Program Files\hashcat-6.2.6\rules\d3ad0ne.rule
99092 C:\Program Files\hashcat-6.2.6\rules\dive.rule
<---snip--->
10 C:\Program Files\hashcat-6.2.6\rules\hybrid\append_d.rule
43 C:\Program Files\hashcat-6.2.6\rules\hybrid\append_ds.rule
44 C:\Program Files\hashcat-6.2.6\rules\hybrid\append_ds_passthrough.rule
36 C:\Program Files\hashcat-6.2.6\rules\hybrid\append_du.rule
69 C:\Program Files\hashcat-6.2.6\rules\hybrid\append_dus.rule
<---snip--->
```

Note the `@`-sign before the `wc` command to prevent the explicit printing of the command.

## Ping sweep

Using a `for` loop to ping sweep a network segment

```bat
for /L %i in (1,1,255) do @ping -n 1 -w 200 192.168.100.%i > nul && echo x.x.x.%i is up.
```

## Resources

for - Microsoft Learn: <https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/for>

for - ss64.com: <https://ss64.com/nt/for.html>
