Parsing Path in a cmd file?

  • Thread starter Thread starter Stephen Quist
  • Start date Start date
S

Stephen Quist

Hi,

I'm trying to write a little command file to see if all the
directories in a PATH exist. I've run across a problem
with spaces in path names.

If I just check the path statement I get a list of names
separated with semicolons:
PATH=c:\perl\bin;c:\cygwin\bin;C:\PROGRA~1\MKSTOO~1\bin;...
This includes elements like this:
C:\Program Files\Common Files\Adaptec Shared\System; with embedded spaces.

When I use PATH in a for statement,
for %%p in (%path%) do (
echo %%p
)


The semicolons get converted to spaces:

for %p in (c:\perl\bin c:\cygwin\bin C:\PROGRA~1\MKSTOO~1\bin

so that when I get to the directory names that do contain spaces

I get what you'd expect, but not what I want:

C:\stuff>(echo C:\Program )
C:\Program

C:\stuff>(echo Files\Common )
Files\Common

C:\stuff>(echo Files\Adaptec )
Files\Adaptec

C:\stuff>(echo Shared\System )
Shared\System


Has anyone here already solved this little problem?



Thanks



Steve
 
Stephen said:
Hi,

I'm trying to write a little command file to see if all the
directories in a PATH exist. I've run across a problem
with spaces in path names.

If I just check the path statement I get a list of names
separated with semicolons:
PATH=c:\perl\bin;c:\cygwin\bin;C:\PROGRA~1\MKSTOO~1\bin;...
This includes elements like this:
C:\Program Files\Common Files\Adaptec Shared\System; with embedded spaces.

When I use PATH in a for statement,
for %%p in (%path%) do (
echo %%p
)


The semicolons get converted to spaces:

for %p in (c:\perl\bin c:\cygwin\bin C:\PROGRA~1\MKSTOO~1\bin

so that when I get to the directory names that do contain spaces

I get what you'd expect, but not what I want:

C:\stuff>(echo C:\Program )
C:\Program

C:\stuff>(echo Files\Common )
Files\Common

C:\stuff>(echo Files\Adaptec )
Files\Adaptec

C:\stuff>(echo Shared\System )
Shared\System


Has anyone here already solved this little problem?



Thanks



Steve

Here's one way:

- - - - - - - - - - begin screen capture WinXP - - - - - - - - - -
C:\cmd>dir /ad \*dir*
Volume in drive C has no label.
Volume Serial Number is B0CC-E24F

Directory of C:\

11/23/2004 03:20 PM <DIR> coadir
03/14/2005 03:36 PM <DIR> junkdir
12/07/2004 01:25 PM <DIR> my dir
03/15/2005 11:09 AM <DIR> some dir
03/15/2005 11:09 AM <DIR> some other dir
0 File(s) 0 bytes
5 Dir(s) 1,766,834,176 bytes free

C:\cmd>demo\ParsePath
"c:\no such dir" does not exist.

C:\cmd>rlist demo\ParsePath.cmd
=====begin C:\cmd\demo\ParsePath.cmd ====================
01. @echo off
02. setlocal
03. set "zpath=c:\coadir;c:\my dir;c:\junkdir;c:\some dir;c:\some other dir;c:\no such dir;c:\windows;"
04. set zpath=%zpath:;=$%
05. :main
06. for /f "tokens=1* delims=$" %%a in (
07. 'echo %zpath%'
08. ) do set first="%%a"&set zpath=%%b
09. if not exist %first%\. echo/%first% does not exist.
10. if defined zpath goto :main
=====end C:\cmd\demo\ParsePath.cmd ====================
- - - - - - - - - - end screen capture WinXP - - - - - - - - - -
 
In said:
Hi,

I'm trying to write a little command file to see if all the
directories in a PATH exist. I've run across a problem
with spaces in path names.

If I just check the path statement I get a list of names
separated with semicolons:
PATH=c:\perl\bin;c:\cygwin\bin;C:\PROGRA~1\MKSTOO~1\bin;...
This includes elements like this:
C:\Program Files\Common Files\Adaptec Shared\System; with
embedded spaces.

When I use PATH in a for statement,
for %%p in (%path%) do (
echo %%p
)


The semicolons get converted to spaces:

for %p in (c:\perl\bin c:\cygwin\bin C:\PROGRA~1\MKSTOO~1\bin

so that when I get to the directory names that do contain spaces

I get what you'd expect, but not what I want:

C:\stuff>(echo C:\Program )
C:\Program

C:\stuff>(echo Files\Common )
Files\Common

C:\stuff>(echo Files\Adaptec )
Files\Adaptec

C:\stuff>(echo Shared\System )
Shared\System


Has anyone here already solved this little problem?

Unless it's an exercise or limited to batch you may find that
Bill Stewart's editpath.exe
can solve your problem

http://internet.cybermesa.com/~bstewart/misctools.html
http://internet.cybermesa.com/~bstewart/files/epath1.zip
 
My thanks to both Mark and Phil.

I ended up borrowing a technique from Matthias Tacke and ended up with
this:

@setlocal
@set BadPath=
for %%A in ("%path:;=";"%") do @if not exist %%A @set BadPath=%BadPath% %%A
@echo non-existent directories: %BadPath%
@endlocal


Steve
 
Back
Top