Travers PATH variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xl1000
Posts: 10
Joined: 01 Feb 2011 06:10

Travers PATH variable

#1 Post by xl1000 » 01 Feb 2011 06:28

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

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Travers PATH variable

#2 Post by ChickenSoup » 01 Feb 2011 09:00

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Travers PATH variable

#3 Post by aGerman » 01 Feb 2011 09:02

This would work for me as well:

Code: Select all

@echo off &setlocal
for %%a in ("%path:;=" "%") do echo %%~a
pause

Regards
aGerman

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Travers PATH variable

#4 Post by ChickenSoup » 01 Feb 2011 09:14

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. :wink:

xl1000
Posts: 10
Joined: 01 Feb 2011 06:10

Re: Travers PATH variable

#5 Post by xl1000 » 01 Feb 2011 09:18

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 !!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Travers PATH variable

#6 Post by aGerman » 01 Feb 2011 15:24

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. :wink:

I'm honored :oops:
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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Travers PATH variable

#7 Post by jeb » 02 Feb 2011 14:47

Thanks for the compliment,
but now I'm forced to show some new impressive solutions. :wink:

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Travers PATH variable

#8 Post by aGerman » 02 Feb 2011 16:37

jeb wrote:Thanks for the compliment,
but now I'm forced to show some new impressive solutions. :wink:

This was my intention.


jeb wrote:Perhaps I should write an AI in batch as one liner,

:lol:


jeb wrote:or maybe I should show the more complex solution of including files in batch :)

Why not :?: :!:

Regards
aGerman

Post Reply