Page 1 of 1

Stop search when exact match found.

Posted: 20 Aug 2020 08:50
by Yanta
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?

Re: Stop search when exact match found.

Posted: 20 Aug 2020 10:25
by Squashman
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

Re: Stop search when exact match found.

Posted: 20 Aug 2020 19:22
by Yanta
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