Doug
Doug

Reputation: 133

Batch - open a file where the filename contains a certain word

So I have a batch file that checks for files in a directory against 4 different file extensions and processes them if they have the correct file extension:

set count=0
for %%x in (*.J_E, *.J_T, *.J_I, *.BCC) do (
  set /a count=count+1
  set choice[!count!]=%%x
)
for /l %%x in (1,1,!count!) do (
   echo %%x. !choice[%%x]!
   tlv2txt !choice[%%x]! > !choice[%%x]!.txt
)

What I would like to do is add the ability to also use files where the filename contains a certain string like 'marmite'. For example..

By running the batch file I would be looking to 'process' the files marked with an asterisk only:

Like I said, I can get the file extensions, but I haven't the foggiest idea how to get filenames that contain.. Is this do-able?

Upvotes: 2

Views: 2233

Answers (2)

prudviraj
prudviraj

Reputation: 3744

Yes, you can achieve it.

dir /a:-D /b | findstr /i "word"

dir /a:-D /b : This command will list all files in the directory.

findstr /i : This command will check for the matching word in all files and lists them

/i : case insensitive search. In our example, we will get a list of filename which have name containing string "word".

Please take this idea and proceed.

Upvotes: 1

Doug
Doug

Reputation: 133

Well, I seem to have answered my own question!

I'll leave the answer here for anyone else who might be experiencing the same problem..

The answer is

*marmite*.* 

So, using the example above;

for %%x in (*.J_E, *.J_T, *.J_I, *.BCC, *marmite*.*) 

Hope this helps someone! :)

Upvotes: 2

Related Questions