I simply need to parse URLs to obtain the file name they ultimately
point to. I have a semi-solution, and it works, but it makes use of a
fixed "tokens" number, and I would prefer not to have to worry about
that number changing.
Here's what I mean, by way of example:
set DownloadURL=http://www.whatever.com/dir1/filename.zip
for /f "tokens=4 delims=/" %%a in ("%DownloadURL%") do set
ZIPFileName=%%a
This works, but if the URL ever becomes
http://www.whatever.com/dir1/dir2/filename.zip (for example), I will
have to remember to change the second line to use "tokens=5".
One possibility is repeatedly stripping off the first such token and
calling the same procedure with the remaining substring. The following
is a simple example, using the convention that batch file lines begin
with two spaces, so other lines have wrapped and need to be rejoined to
the line(s) above.
@echo off & setlocal
set /P url=Enter url:
call :STRIP "%url%"
echo filename: "%fn%"
goto :EOF
:STRIP
for /F "tokens=1,* delims=/" %%a in (%1) do (
if "%%b" == "" (
set fn=%~1
) else (
call :STRIP "%%b"
)
)
goto :EOF
> This works, but if the URL ever becomes
> http://www.whatever.com/dir1/dir2/filename.zip (for example), I will
> have to remember to change the second line to use "tokens=5".
(Assuming no spaces)
@echo off & setlocal enableextensions
set url_=http://www.whatever.com/dir1/dir2/filename.zip
for /f "tokens=1-8 delims=/" %%a in ('echo %url_%') do (
if not "%%a"=="" set filename_=%%a
if not "%%b"=="" set filename_=%%b
if not "%%c"=="" set filename_=%%c
if not "%%d"=="" set filename_=%%d
if not "%%e"=="" set filename_=%%e
if not "%%f"=="" set filename_=%%f
if not "%%g"=="" set filename_=%%g
if not "%%h"=="" set filename_=%%h
)
echo %filename_%
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
Watch for wrapped lines, new lines start with a number, rememve the
line number and merge wrapped lines before executing the script.
===Script: URL.cmd===
001. @echo off
002. set url=http://www.google.com/dir1/dir2.zip/dir3/filename3.zip
003. for /f "tokens=*" %%a in ("%url%") do set archivename=%%~na%%~xa
004. echo %archivename%===End Script: URL.cmd===
===End script; URL.cmd===
I'm still testing, but here's the output:
here's the output:
C:\test\url>echo %url%
http://www.google.com/dir1/dir2.zip/dir3/filename3.zip
C:\test\url>type test.cmd
@echo off
set url=http://www.google.com/dir1/dir2.zip/dir3/filename3.zip
for /f "tokens=*" %%a in ("%url%") do set archivename=%%~na%%~xa
echo %archivename%
C:\test\url>test
filename3.zip
C:\test\url>
Hope that works for you :)
can be combined to read:
003. for /f "tokens=*" %%a in ("%url%") do set archivename=%%~nxa
sorry.
> One possibility is repeatedly stripping off the first such token and
> calling the same procedure with the remaining substring.
<snip>
Thank you! It works.
> I think this would work for you.
> for /f "tokens=*" %%a in ("%url%") do set archivename=%%~nxa
<snip>
Very cool! This is what I was after.
It's interesting to see all the different ways of solving the same
problem... I've noted and APPRECIATED them all. Thank you!
<snip>
Thank you!
That is typical of script solutions and characteristic of this
newsgroupand the many participants. You will also note on a closer
inspection that the solutions often vary in the level of their
generality. Some solutions are strictly customized, some are more
adaptable across the gamut.
It doesn't have to be that complicated:
for %%A in ( %URL% ) do set archivename=%%~nxA
--
T.E.D. (tda...@gearbox.maem.umr.edu)
There is an useful colollary. 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
The ~nx bit does nothing. The same result would be achieved by the for
loop
for %%a in (%line_%) do set line_=%%a
Indeed, if the last token ended with a period, and the period should be
included in the result, the ~nx would remove it.
set x=This is a test.
for %%a in (%x%) do set y=%%~nxa
echo %y%
produces
test
while
set x=This is a test.
for %%a in (%x%) do set y=%%a
echo %y%
produces
test.
> The ~nx bit does nothing. The same result would be achieved by the for
> loop
>
> for %%a in (%line_%) do set line_=%%a
>
> Indeed, if the last token ended with a period, and the period should be
> included in the result, the ~nx would remove it.
That, indeed, is the core of the matter. Taken the other way round
line_=%%a does not remove the ending period. Since what is desirable
depends on the circumstances, both the solutions are useful. I'll be
adding the information to the FAQ item I am writing. Thanks for
bringing this up.
end of Timo
end of Timo.
end of Timo.Salmi
The second line would be treated differently.