Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I decompose the path to be one folder per line?

19 views
Skip to first unread message

Timo Salmi

unread,
Sep 9, 2005, 4:50:09 PM9/9/05
to
DRAFT:
115) How can I decompose the path to be one folder per line?

@echo off & setlocal enableextensions
echo %path%;^
|gawk -F; "{for (i=1;i<=NF-1;++i) printf \"%%s\n\",$i}"
endlocal & goto :EOF

The output might be e.g.
C:\_D\TEST>cmdfaq
c:\_f\xtools
c:\_f\tools
c:\_f\ftools
c:\_e\arczip
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\Program Files\PC-Doctor for Windows\services
C:\Program Files\TSEpro
C:\Program Files\010Editor

Or with straight script
@echo off & setlocal enableextensions
for /f "tokens=1-15 delims=;" %%a in ("%path%") do (
if not [%%a]==[] echo %%a
if not [%%b]==[] echo %%b
if not [%%c]==[] echo %%c
if not [%%d]==[] echo %%d
if not [%%e]==[] echo %%e
if not [%%f]==[] echo %%f
if not [%%g]==[] echo %%g
if not [%%h]==[] echo %%h
if not [%%i]==[] echo %%i
if not [%%j]==[] echo %%j
if not [%%k]==[] echo %%k
if not [%%l]==[] echo %%l
if not [%%m]==[] echo %%m
if not [%%n]==[] echo %%n
if not [%%o]==[] echo %%o
)
endlocal & goto :EOF

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Mark V

unread,
Sep 9, 2005, 5:12:12 PM9/9/05
to

Nice, and although it is not to the same purpose I would mention
the Bill Stewert editpath.exe utility as on topic in terms of
results for those that may be interested.

editpath -l -s -x
for example.

http://internet.cybermesa.com/~bstewart/misctools.html
http://internet.cybermesa.com/~bstewart/files/epath1.zip

William Allen

unread,
Sep 9, 2005, 5:23:09 PM9/9/05
to
"Timo Salmi" wrote in message

> DRAFT:
> 115) How can I decompose the path to be one folder per line?
...snip

> Or with straight script
> @echo off & setlocal enableextensions
> for /f "tokens=1-15 delims=;" %%a in ("%path%") do (
> if not [%%a]==[] echo %%a
> if not [%%b]==[] echo %%b
> if not [%%c]==[] echo %%c
> if not [%%d]==[] echo %%d
> if not [%%e]==[] echo %%e
> if not [%%f]==[] echo %%f
> if not [%%g]==[] echo %%g
> if not [%%h]==[] echo %%h
> if not [%%i]==[] echo %%i
> if not [%%j]==[] echo %%j
> if not [%%k]==[] echo %%k
> if not [%%l]==[] echo %%l
> if not [%%m]==[] echo %%m
> if not [%%n]==[] echo %%n
> if not [%%o]==[] echo %%o
> )
> endlocal & goto :EOF

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
SET P=%PATH%
:LOOP
FOR /f "tokens=1* delims=;" %%A IN ("%P%") DO (
ECHO/%%A
SET P=%%B
IF DEFINED P GOTO LOOP
)

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/


Clay Calvert

unread,
Sep 9, 2005, 7:10:57 PM9/9/05
to
On Fri, 9 Sep 2005 20:50:09 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:

>DRAFT:
>115) How can I decompose the path to be one folder per line?


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


or even:

@for %%a in ("%path:;=" "%") do @echo %%~a

Thanks,
Clay Calvert
CCal...@Wanguru.com
Replace "W" with "L"

Timo Salmi

unread,
Sep 9, 2005, 8:15:30 PM9/9/05
to
In article <4321fd74$0$17479$ed2e...@ptn-nntp-reader04.plus.net>,

William Allen <_w...@email.com> wrote:
> "Timo Salmi" wrote in message
> > DRAFT:

> ====Begin cut-and-paste (omit this line)
> @ECHO OFF
(snip)

Excellent! Tested with success and a Google repository pointer to
your posting added to this FAQ item.

Timo Salmi

unread,
Sep 9, 2005, 8:20:51 PM9/9/05
to
In article <ha54i15ju5b3ek2qh...@4ax.com>,

Clay Calvert <ccal...@Wanguru.com> wrote:
> t...@uwasa.fi (Timo Salmi) wrote:
> >DRAFT:

> for %%a in ("%path:;=" "%") do echo %%~a

Now that is truly inventive! Tested with success and a Google


repository pointer to your posting added to this FAQ item.

All the best, Timo

Timo Salmi

unread,
Sep 9, 2005, 9:09:57 PM9/9/05
to
In article <ha54i15ju5b3ek2qh...@4ax.com>,
Clay Calvert <ccal...@Wanguru.com> wrote:
> for %%a in ("%path:;=" "%") do echo %%~a

Let expand on your inventive tip. It gives the key to e.g. listing
all the *.BAT files which apprear at the path:

@echo off & setlocal enableextensions

for %%a in ("%path:;=" "%") do (
for /f %%f in ('dir /b /a:-d /o:n %%~a\*.bat') do (
echo %%~a\%%f)
)>>%temp%\temp.dir
for %%c in (type del) do call %%c %temp%\temp.dir

Clay Calvert

unread,
Sep 9, 2005, 11:20:33 PM9/9/05
to
On Sat, 10 Sep 2005 00:20:51 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:

>> for %%a in ("%path:;=" "%") do echo %%~a


>
>Now that is truly inventive! Tested with success and a Google
>repository pointer to your posting added to this FAQ item.

Thank you very much. 'Tis an honor.

Peace,
Clay

Clay Calvert

unread,
Sep 9, 2005, 11:20:50 PM9/9/05
to
On Sat, 10 Sep 2005 01:09:57 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:

>Let expand on your inventive tip. It gives the key to e.g. listing


>all the *.BAT files which apprear at the path:

How about?

@echo off


for %%a in ("%path:;=" "%") do (

for %%g in ("%%~a\*.bat") do echo %%~fg)


Note the "%%~a\*.bat" should be quoted.

Thanks again,

Timo Salmi

unread,
Sep 10, 2005, 1:05:35 AM9/10/05
to
In article <m2k4i1luek2th2087...@4ax.com>,

Clay Calvert <ccal...@Wanguru.com> wrote:
> t...@uwasa.fi (Timo Salmi) wrote:
> >Let expand on your inventive tip. It gives the key to e.g. listing
> >all the *.BAT files which apprear at the path:

> for %%a in ("%path:;=" "%") do (


> for %%g in ("%%~a\*.bat") do echo %%~fg)

That is clarly better than what I posted since it avoids the "File
not found" problem on folders that contain no *.BAT files. Well
done.

I think that this might deserve an item of its own in the FAQ.

Clay Calvert

unread,
Sep 10, 2005, 8:49:54 AM9/10/05
to
On Sat, 10 Sep 2005 05:05:35 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:

>I think that this might deserve an item of its own in the FAQ.

Defnitely an honor.

Since it looks like you may be heading in this direction. Here is a
"which" routine I originally wrote 4 years ago, but it mandated no
extension in the argument. This is a re-write in which (pun intended)
extensions are optional.

@echo off&echo.
(set WF=)
for %%a in ("" %PATHEXT:;= %) do (
if "%WF%"=="" if exist "%~1%%~a" set WF=%CD%\%~1%%~a)
for %%a in ("" %PATHEXT:;= %) do (
if "%WF%"=="" for %%g in ("%~1%%~a") do (
if exist "%%~$PATH:g" set WF=%%~$PATH:g))
if defined WF (echo %WF%) else echo "%~1" Not Found

Test with strings such as "Notepad" or "win".

Now, if we can just get a routine to enumerate internal commands.
Actually, I've got an idea for that but I have to run "literally" now.

As an aside, it easy to become fond of the "/f" switch in "for". Often
the standard capabilities of that command don't even register as an
option.

Thanks

Timo Salmi

unread,
Sep 10, 2005, 10:03:19 AM9/10/05
to
In article <nbh5i1tglbr6o3vml...@4ax.com>,

Clay Calvert <ccal...@Wanguru.com> wrote:
> t...@uwasa.fi (Timo Salmi) wrote:
(snip)

> Here is a
> "which" routine I originally wrote 4 years ago, but it mandated no
> extension in the argument. This is a re-write in which (pun intended)
> extensions are optional.

Also a nice one. Tested. Definitely worth a Google link my FAQ in
the path item.

Timo Salmi

unread,
Sep 10, 2005, 10:33:46 AM9/10/05
to
Clay Calvert <ccal...@Wanguru.com> wrote:
> Since it looks like you may be heading in this direction. Here is a
> "which" routine I originally wrote 4 years ago, but it mandated no
> extension in the argument. This is a re-write in which (pun intended)
> extensions are optional.

Apropos, I have the following solution in my FAQ as item #46, but it
requires the extension.

46) Is a program available in the default folder or at path?

A counterpart of the UNIX "which" command.
@echo off & setlocal enableextensions enabledelayedexpansion
::
if [%1]==[] (
echo Usage: %~0 [Filename]
goto :EOF)
::
if not [%2]==[] (
echo Usage for long file names: %~0 ["Filename"]
goto :EOF)
::
set target_=%~1
::
for %%f in ("%target_%") do set where_="%%~$PATH:f"
if exist "%target_%" set where_="%cd%\%target_%"
if [%where_%]==[""] (
echo "%target_%" not found at path or current folder
) else (
set where_=!where_:"=!
echo where_=!where_!)
::
endlocal & goto :EOF

Clay Calvert

unread,
Sep 13, 2005, 10:46:10 PM9/13/05
to
On Sat, 10 Sep 2005 14:33:46 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:

>Clay Calvert <ccal...@Wanguru.com> wrote:


>> Since it looks like you may be heading in this direction. Here is a
>> "which" routine I originally wrote 4 years ago, but it mandated no
>> extension in the argument. This is a re-write in which (pun intended)
>> extensions are optional.
>
>Apropos, I have the following solution in my FAQ as item #46, but it
>requires the extension.
>
>46) Is a program available in the default folder or at path?
>
>A counterpart of the UNIX "which" command.
> @echo off & setlocal enableextensions enabledelayedexpansion

So far, this "which" seems to work without, and with, extensions, and
is fairly accurate at identifying internal commands. Hopefully, it
will work with localized operating systems, and it can probably be
easily modified to work with a 64-bit OS.

@echo off&echo.
(set WF=)

for %%a in ("" %PATHEXT:;= %) do (
if "%WF%"=="" if exist "%~1%%~a" set WF=%CD%\%~1%%~a)

for %%a in ("" %PATHEXT:;= %) do (
if "%WF%"=="" for %%g in ("%~1%%~a") do (
if exist "%%~$PATH:g" set WF=%%~$PATH:g))

if NOT "%~x1"=="" goto:END
for /f "delims=." %%a in ('help.exe *') do set HlpErr=%%a

help.exe "%~1" |find "%HlpErr%">nul && goto:END

for %%a in (exe com) do (
if /i "%WF%"=="%windir%\system32\%~1.%%a" goto:END)

set WF="%1" is an internal command

:END
if defined WF (echo %WF%) else (
echo The file: "%~1" was not found)

Thanks,

Timo Salmi

unread,
Sep 13, 2005, 11:14:06 PM9/13/05
to
Clay Calvert <ccal...@Wanguru.com> wrote:
> t...@uwasa.fi (Timo Salmi) wrote:
> >46) Is a program available in the default folder or at path?
> >A counterpart of the UNIX "which" command.

> So far, this "which" seems to work without, and with, extensions, and


> is fairly accurate at identifying internal commands. Hopefully, it

(snip the code)

Clay, is this effectively the same code you posted earlier, or is it
a new version needing an updated pointer in my FAQ? The one I
included in the said item is

References/Comments:
http://www.google.com/groups?selm=nbh5i1tglbr6o3vml...@4ax.com

Clay Calvert

unread,
Sep 14, 2005, 5:47:08 AM9/14/05
to
On Wed, 14 Sep 2005 03:14:06 +0000 (UTC), t...@uwasa.fi (Timo Salmi)
wrote:


>> So far, this "which" seems to work without, and with, extensions, and
>> is fairly accurate at identifying internal commands. Hopefully, it
>
>(snip the code)
>
>Clay, is this effectively the same code you posted earlier, or is it
>a new version needing an updated pointer in my FAQ? The one I
>included in the said item is

It is a newer version. This version should properly identify internal
commands, such as CD, DIR, BREAK and VERIFY.

Clay Calvert

unread,
Sep 14, 2005, 6:31:32 PM9/14/05
to
On Wed, 14 Sep 2005 05:47:08 -0400, Clay Calvert
<ccal...@Wanguru.com> wrote:

>>Clay, is this effectively the same code you posted earlier, or is it
>>a new version needing an updated pointer in my FAQ? The one I
>>included in the said item is
>
>It is a newer version. This version should properly identify internal
>commands, such as CD, DIR, BREAK and VERIFY.

Even newer version, with a serious bug removed.

@echo off&echo.
(set WF=)

for %%a in ("" %PATHEXT:;= %) do (

if not defined WF if exist "%~1%%~a" set WF=%CD%\%~1%%~a)

for %%a in ("" %PATHEXT:;= %) do (

if not defined WF for %%g in ("%~1%%~a") do (


if exist "%%~$PATH:g" set WF=%%~$PATH:g))

if NOT "%~x1"=="" goto:END
for /f "delims=." %%a in ('help.exe *') do set HlpErr=%%a

help.exe "%~1" |find "%HlpErr%">nul && goto:END

for %%a in (exe com) do (
if /i "%WF%"=="%windir%\system32\%~1.%%a" goto:END)

set WF="%~1" is an internal command

:END
if defined WF (echo. { %WF% }) else (


echo The file: "%~1" was not found)

Thanks and Sorry Timo,
Clay

Timo Salmi

unread,
Sep 14, 2005, 11:34:03 PM9/14/05
to
Clay Calvert <ccal...@Wanguru.com> wrote:
> Even newer version, with a serious bug removed.
> @echo off&echo.
(snip)

Ok, the Message-Id captured.

0 new messages