schneiju
schneiju

Reputation: 125

For loop wildcard matching in batch script

I'm writing a batch script that is supposed to find all files in a directory of type .exp and rename them. Everything is working except the FOR loop, because I can't seem to get the wildcard matching to find the proper set of files.

If I write:

for /F %%x in (*.exp) do (echo %%x)

Result is:

The system cannot find the file *.exp

If I write:

for /F %%x in ("*.exp") do (
echo %%x
echo %%~nx.exp
)

Result is:

*.exp
expectedfilename1.exp

but the loop only runs once and stops at the first file.

Every online example I've seen uses one of these formats, so I have no idea what I'm doing wrong. Help is much appreciated!

Upvotes: 0

Views: 1001

Answers (1)

Matt Williamson
Matt Williamson

Reputation: 7095

Try

For %%x in (*.exp) do (echo %%x)

Or

For /f "tokens=*" %%x in ('dir /b *.exp') do (echo %%x)

Upvotes: 1

Related Questions