<http://www.netikka.net/tsneti/info/tscmd142.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sat 28-Jan-2017 12:40:19

 
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.



142} How to extract the filename from the end of an URL reference?

There are several ways of doing this. Consider the following example where the URL is http://www.netikka.net/tsneti/opas/retirereply.php. The easiest of the solutions, posted by Ted Davis et al. is

  @echo off & setlocal enableextensions
  set url_=http://www.netikka.net/tsneti/opas/retirereply.php
  for %%a in (%url_%) do set filename_=%%~nxa
  echo %filename_%
  endlocal & goto :EOF

The output would be
  C:\_D\TEST>cmdfaq
  retirereply.php

Note that if there are spaces (not a good idea in an URL), the URL has to be enclosed in quotes.

There is an interesting corollary. The method can also be used to get the last word on a line. For example
  @echo off & setlocal enableextensions
  set line_=Hello World and Ted Davis
  for %%a in (%line_%) do set line_=%%~nxa
  echo %line_%
  endlocal & goto :EOF

will give
  C:\_D\TEST>cmdfaq
  Davis

However, consider
  @echo off & setlocal enableextensions
  set line_=Hello World and Harlan Grove.
  for %%a in (%line_%) do set line_=%%~a
  echo %line_%
  for %%a in (%line_%) do set line_=%%~nxa
  echo %line_%
  endlocal & goto :EOF

Note the differences in the output
  C:\_D\TEST>cmdfaq
  Grove.
  Grove

Also see item #150.


References/Comments:
  Google Groups Jun 1 2006, 12:51 am [M]
  Google Groups Jun 1 2006, 3:36 am [M]
  Google Groups Jun 1 2006, 8:37 am [M]