C:\_G\WWW\~ELISANET\INFO\tscmd157.html
<http://www.elisanet.fi/tsalmi/info/tscmd157.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Thu 29-Nov-2018 13:39:40

 
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.



157} How to get the count of words and the last word of a variable?

  @echo off & setlocal enableextensions
  set var=This is my test
  call :GetLast "%var%" lastword nr
  ::
  :: Display the result

  echo.%var%
  echo The number of words is %nr%
  echo The last word is "%lastword%"
  endlocal & goto :EOF
  ::
  :GetLast
  setlocal
  set rest_=%~1
  :_loop
  for /f "tokens=1*" %%a in ("%rest_%") do (
    set /a count_+=1
    set rest_=%%b
    set last_=%%a
    if defined rest_ goto _loop
    )
  endlocal & set "%2=%last_%" & set "%3=%count_%" & goto :EOF

The output will be
  C:\_D\TEST>cmdfaq
  This is my test
  The number of words is 4
  The last word is "test"

Note the following, obvious twist
  @echo off & setlocal enableextensions
  set var=This is, my test
  call :GetLast "%var%" lastword nr
  ::
  :: Display the result

  echo.%var%
  echo The number of words is %nr%
  echo The last word is "%lastword%"
  endlocal & goto :EOF
  ::
  :GetLast
  setlocal
  set rest_=%~1
  :_loop
  for /f "tokens=1* delims=," %%a in ("%rest_%") do (
    set /a count_+=1
    set rest_=%%b
    set last_=%%a
    if defined rest_ goto _loop
    )
  endlocal & set "%2=%last_%" & set "%3=%count_%" & goto :EOF

The output will be
  C:\_D\TEST>cmdfaq
  This is, my test
  The number of words is 2
  The last word is " my test"

Consider an application with several twists. A user wrote (suitably edited here) in alt.msdos.batch.nt

I (the said user) have this On_Call.txt file with the following information
  22.01.2007     John Doe           John.doe@mytest.com
  9991234567@mytest.com
  22.01.2007     Yo C.J. Joe         Yo.Joe@mytest.com
  9991236789@mytest.com
  29.01.2007     Mary Jane         Mary.Jane@mytest.com
  9994567890@mytest.com
  29.01.2007     John Jon            John.jon@mytest.com
  9997890123@mytest.com
  05.02.2007     Tina Turner       Tina.Turner@mytest.com
  9998901234@mytest.com
  05.02.2007     Ike Turner         Ike.Turner@mytest.com
  9999012345@mytest.com

I need help with a script to read this file and only pull the email address and pager number and write them or export to another file based on the date. I have two files called email.txt which only has email address and pager.txt that has only the pager number it. Incidently, the above file is automatically generated by another system and saved as a text file in the above format.

So lets say that today is 22.01.2007, the script would run and only export from the above file John.doe@mytest.com and Yo.Joe@mytest.com and write them to the email.txt file and then take the pager numbers 9991234567@mytest.com and 9991236789@mytest.com and write them to the pager.txt file.

This is Timo's solution
  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Delete the old situation, if need be. Else not.

  if exist email.txt del email.txt
  if exist pager.txt del pager.txt
  ::
  set today=%date%
  for /f "tokens=*" %%a in ('type On_Call.txt') do (
    set /a count+=1
    set /a mod=!count!%%2
    if !mod! EQU 1 (
      set entireline=%%a
      set flag=
      for /f "tokens=1" %%a in ('echo !entireline!') do (
        if "%%a"=="%today%" set flag=true
        )
      if defined flag (
        call :GetLast "!entireline!" lastword
        echo !lastword!
        )>>email.txt
      )
    if defined flag if !mod! EQU 0 echo %%a>>pager.txt
    )
  ::
  :: Display the results

  type email.txt
  echo.
  type pager.txt
  endlocal & goto :EOF
  ::
  :: The subroutine for getting the last item on a line

  :GetLast
  setlocal
  set rest_=%~1
  :_loop
  for /f "tokens=1*" %%a in ("%rest_%") do (
    set rest_=%%b
    set last_=%%a
    if defined rest_ goto _loop
    )
  endlocal & set "%2=%last_%" & goto :EOF

The test output
  C:\_M>test.cmd
  John.doe@mytest.com
  Yo.Joe@mytest.com

  9991234567@mytest.com
  9991236789@mytest.com

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd157.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd157
http://www.elisanet.fi/tsalmi/info/tscmd157.html
file:///c:/_g/www/~elisanet/info/tscmd157.html