Page 1 of 1
Travers PATH variable
Posted: 01 Feb 2011 06:28
by xl1000
I´m trying to traverse the PATH variable for each folder to see if a specific file is found.
Until now i´ve not succeeded to do this, and i´m wondering if someone else has a solution to this.
Sample, when the PATH variable has the content:
PATH=C:\Program Files\Application\bin;C:\PROGRA~1\APP2\EXEC;C:\Windows\System32
Can it be expanded to show on each line like :
C:\Program Files\Application\bin
C:\PROGRA~1\APP2\EXEC
C:\Windows\System32
Re: Travers PATH variable
Posted: 01 Feb 2011 09:00
by ChickenSoup
Try this:
Code: Select all
@echo off
set pathvar=%path%
set searchfile=cmd.exe
:pathloop
for /f "tokens=1* delims=;" %%a in ("%pathvar%") do (
echo Searching for %searchfile% in: %%a
dir /b "%%a" 2>nul|findstr /i /r "\<%searchfile%\>">nul && echo.%searchfile% Found in %%a
set pathvar=%%b
)
if "%pathvar%"=="" goto endloop
goto pathloop
:endloop
Not sure if it is the best way to do it... but, it works.
Re: Travers PATH variable
Posted: 01 Feb 2011 09:02
by aGerman
This would work for me as well:
Code: Select all
@echo off &setlocal
for %%a in ("%path:;=" "%") do echo %%~a
pause
Regards
aGerman
Re: Travers PATH variable
Posted: 01 Feb 2011 09:14
by ChickenSoup
aGerman wrote:This would work for me as well:
Code:
@echo off &setlocal
for %%a in ("%path:;=" "%") do echo %%~a
pause
I yeild to the master of batch.
Re: Travers PATH variable
Posted: 01 Feb 2011 09:18
by xl1000
Hi Guys...
Both of you, great solutions !!
ChickenSoup, more extensive, and already prepared for doing other stuff
Though i always have difficulty with traversing a variable like you do, continuing until its empty.
aGerman, quick to the point and straigh forward. superb !!
Thank you guys, both of you !!
Re: Travers PATH variable
Posted: 01 Feb 2011 15:24
by aGerman
xl1000 wrote:aGerman, quick to the point and straigh forward. superb !!
Thanks, but sometimes I'm much too "straight forward". At work I never have enough time for explanations and work-arounds
ChickenSoup wrote:I yeild to the master of batch.
I'm honored
I would not hesitate to declare jeb the "wise man of batch". I never found more new ideas and excelent solutions than in his postings.
Regards
aGerman
Re: Travers PATH variable
Posted: 02 Feb 2011 14:47
by jeb
Thanks for the compliment,
but now I'm forced to show some new impressive solutions.
Perhaps I should write an AI in batch as one liner,
or maybe I should show the more complex solution of including files in batch
jeb
Re: Travers PATH variable
Posted: 02 Feb 2011 16:37
by aGerman
jeb wrote:Thanks for the compliment,
but now I'm forced to show some new impressive solutions.
This was my intention.
jeb wrote:Perhaps I should write an AI in batch as one liner,
jeb wrote:or maybe I should show the more complex solution of including files in batch
Why not
Regards
aGerman