Reputation: 3793
I try to create bat file to iterate throuth only files in directory which file names begin with specific word.
For Example:
companyName.module1.exe
companyName.module2.dll
I know how to iterate files but don't know how to check their names for specific condition.
Upvotes: 0
Views: 205
Reputation: 5823
Add the following to your batch file:
for /F "eol=: tokens=*" %%A in ('dir /A-D /B "companyName*"') do (echo %%~fA)
This script will echo
all files (files only) that begin with companyName prefix in the current working directory. Replace echo
with any other command or commands that you want to perform instead.
Update 1: In order to look in a different directory you can either
for /F "eol=: tokens=*" %%A in ('dir /A-D /B "pathToLookIn/companyName*"') do (echo %%~fA)
or
pushd \ & cd "pathToLookIn" & (for /F "eol=: tokens=*" %%A in ('dir /A-D /B "companyName*"') do (echo %%~fA)) & popd
Where pathToLookIn is a fully qualified or relative path.
Update 2: I've updated the for /F
loop to handle file names that begin with ;
as @dbenham suggested.
Upvotes: 1
Reputation: 354356
Well, if you know how to iterate files, then just use that knowledge:
for %%f in (companyName*) do (
...
)
Note that iterating over the output of dir
is error-prone and will mangle Unicode characters in many cases. Since for
is capable of iteration directly there's rarely a need to use a inferior option.
Upvotes: 1