Stop search when exact match found.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Yanta
Posts: 48
Joined: 01 Sep 2019 07:08

Stop search when exact match found.

#1 Post by Yanta » 20 Aug 2020 08:50

I have a text file with the following...

Code: Select all

Cities Skylines,I:\Games\Cities Skylines Mass Transit
Mass Effect,I:\Program Files (x86)\Mass Effect
Mass Effect Andromeda,I:\Program Files (x86)\Mass Effect Andromeda
Mass Effect 3,I:\Program Files (x86)\EA\Mass Effect 3
I search the file with....

Code: Select all

for /F "Tokens=2 delims=," %%a in ('findstr /C:"Mass Effect" "%SrcPath%\folders.txt"') do set GamePath=%%a
Gamepath=I:\Program Files (x86)\EA\Mass Effect 3

when it should be

GamePath=I:\Program Files (x86)\Mass Effect

How to stop search when first exact match found?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Stop search when exact match found.

#2 Post by Squashman » 20 Aug 2020 10:25

Use the comma to your advantage. Use it with the string you are searching for with the FINDSTR command.

Code: Select all

for /F "Tokens=2 delims=," %%a in ('findstr /C:"Mass Effect," "%SrcPath%\folders.txt"') do set GamePath=%%a
Another option would be to use both tokens to your advantage. See if the first token is equal to what you want.

Code: Select all

for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Mass Effect" set GamePath=%%b

Yanta
Posts: 48
Joined: 01 Sep 2019 07:08

Re: Stop search when exact match found.

#3 Post by Yanta » 20 Aug 2020 19:22

Legendary stuff.
For some reason I struggle to get my head around the FOR command and assigning variables to stuff. I don't know why. I don't usually have learning disabilities :)
thanks

Post Reply