Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extracting File Name from URL

69 views
Skip to first unread message

null

unread,
May 31, 2006, 5:03:55 PM5/31/06
to
I just know this has a simple solution... But it's not coming to me.

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".


Harlan Grove

unread,
May 31, 2006, 5:38:24 PM5/31/06
to
null wrote...
...

>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

Timo Salmi

unread,
May 31, 2006, 5:51:35 PM5/31/06
to
null wrote:
> 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".

(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

zackrspv

unread,
May 31, 2006, 5:59:13 PM5/31/06
to
I think this would work for you. I'm sure someone could correct me
but, perhaps i'm actually right for a change:

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 :)

zackrspv

unread,
May 31, 2006, 6:18:01 PM5/31/06
to
And, yes, before i get sharply corrected:

> 003. for /f "tokens=*" %%a in ("%url%") do set archivename=%%~na%%~xa

can be combined to read:

003. for /f "tokens=*" %%a in ("%url%") do set archivename=%%~nxa

sorry.

null

unread,
May 31, 2006, 6:24:29 PM5/31/06
to
Harlan Grove wrote:

> One possibility is repeatedly stripping off the first such token and
> calling the same procedure with the remaining substring.

<snip>

Thank you! It works.


null

unread,
May 31, 2006, 6:26:41 PM5/31/06
to
zackrspv wrote:

> 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!


null

unread,
May 31, 2006, 6:27:27 PM5/31/06
to
Timo Salmi wrote:

<snip>

Thank you!


Timo Salmi

unread,
May 31, 2006, 6:33:07 PM5/31/06
to
null wrote:
> It's interesting to see all the different ways of solving the same
> problem... I've noted and APPRECIATED them all. 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.

Ted Davis

unread,
May 31, 2006, 8:36:11 PM5/31/06
to

It doesn't have to be that complicated:
for %%A in ( %URL% ) do set archivename=%%~nxA

--
T.E.D. (tda...@gearbox.maem.umr.edu)

Timo Salmi

unread,
Jun 1, 2006, 12:47:53 AM6/1/06
to
Ted Davis <tda...@gearbox.maem.umr.edu> wrote:
> for %%A in ( %URL% ) do set archivename=%%~nxA

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

Harlan Grove

unread,
Jun 1, 2006, 1:37:16 AM6/1/06
to
Timo Salmi wrote...
...

>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
...

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.

Timo Salmi

unread,
Jun 1, 2006, 3:56:41 AM6/1/06
to
Harlan Grove wrote:
> Timo Salmi wrote...

>> set line_=Hello World and Ted Davis
>> for %%a in (%line_%) do set line_=%%~nxa

> 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.

0 new messages