<http://www.netikka.net/tsneti/info/tscmd144.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Mon 17-May-2010 12:57:12

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



144} How do I parse the items from a path like C:\a\b\c\d ?

Also see the similar items
   40} How to get the number of and parse the arguments given to a script?
   80} How can I extract the last part of a path such as C:\ABC\DEF\GHI\?
  115} How can I decompose the path to be one folder per line?

Edited from the original question by Bob Altman in alt.msdos.batch.nt
I call a batch file like this: DoSomething c:\a\b\c\d
I need to parse the specified path and execute a command for each folder in the path containing the specified file. The order of execution doesn't matter. So, for the above example, I need to do the following:
  SomeCommand c:\a
  SomeCommand c:\a\b
  SomeCommand c:\a\b\c

With a generic rather than customized parsing view we can use e.g.
  @echo off & setlocal enableextensions enabledelayedexpansion
  set var=C:\a\b\c\d
  rem the above might alternatively be:
  rem set var=%~1
  ::
  :: Parse
  set count_=0
  set rest_=%var%
  :_loop
  for /f "tokens=1* delims=\" %%a in ("%rest_%") do (
    set /a count_+=1
    set var[!count_!]=%%a
    set rest_=%%b
    if defined rest_ goto _loop
    )
  ::
  :: Display the individual parts
  for /l %%i in (1,1,%count_%) do echo var[%%i]=!var[%%i]!
  echo.
  ::
  :: Display combined
  for /l %%i in (1,1,%count_%) do (
    set composite_=!composite_!!var[%%i]!\
    if %%i GTR 1 echo Whatever !composite_:~0,-1!
    )
  ::
  endlocal & goto :EOF

The output would be
  C:\_D\TEST>cmdfaq
  var[1]=C:
  var[2]=a
  var[3]=b
  var[4]=c
  var[5]=d

  Whatever C:\a
  Whatever C:\a\b
  Whatever C:\a\b\c
  Whatever C:\a\b\c\d

Note how the solution also gives the method for counting the number of words in a variable or on a line
  @echo off & setlocal enableextensions
  set var=%~1
  ::
  :: Parse and count

  set count_=0
  set rest_=%var%
  :_loop
  for /f "tokens=1*" %%a in ("%rest_%") do (
    set /a count_+=1
    set rest_=%%b
    if defined rest_ goto _loop
    )
  ::
  :: Display the result

  echo.%var%
  echo The number of words is %count_%
  endlocal & goto :EOF

For example
  C:\_D\TEST>cmdfaq "Asterisk means the remaining text on the line"
  Asterisk means the remaining text on the line
  The number or words is 8

The solution also gives the method to get the last word of a line:
  @echo off & setlocal enableextensions
  set var=%~1
  ::
  :: Parse

  set rest_=%var%
  :_loop
  for /f "tokens=1*" %%a in ("%rest_%") do (
    set rest_=%%b
    set last_=%%a
    if defined rest_ goto _loop
    )
  ::
  :: Display the result

  echo.%var%
  echo The last word is "%last_%"
  endlocal & goto :EOF

For example
  C:\_D\TEST>cmdfaq "Asterisk means the remaining text on the line"
  Asterisk means the remaining text on the line
  The last word is "line"