[SOLVED] How to get filename from URL and set it to variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shohreh
Posts: 25
Joined: 26 Feb 2020 08:05

[SOLVED] How to get filename from URL and set it to variable?

#1 Post by Shohreh » 11 Aug 2020 05:27

Hello,

In a batch file, I need to rip the filename from a URL, and set it to variable.

This doesn't work ("| was unexpected at this time."):

Code: Select all

REM Usage: mybatch.bat http://www.acme.com/myfile.html → extract "myfile.html"

REM OK echo %1 | sed "s/.*\///"

for /f %%i in ('echo %1 | sed "s/.*\///"') do set FILENAME=%%i

echo %FILENAME%
Do you know of a solution, using either cmd, GNU grep/sed/awk?

Thank you.
Last edited by Shohreh on 11 Aug 2020 10:43, edited 1 time in total.

siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Re: How to get filename from URL and set it to variable?

#2 Post by siberia-man » 11 Aug 2020 06:33

replace

Code: Select all

for /f %%i in ('echo %1 | sed "s/.*\///"') do set FILENAME=%%i
with

Code: Select all

for /f %%i in ('echo %1 ^| sed "s/.*\///"') do set FILENAME=%%i

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get filename from URL and set it to variable?

#3 Post by aGerman » 11 Aug 2020 10:32

Why so complicated? Parameter variables support modifiers to extract file name and extension out of the box.

Code: Select all

set "FILENAME=%~nx1"
Refer to the help message of CALL
https://www.dostips.com/DosCommandIndex.php#CALL

Steffen

Shohreh
Posts: 25
Joined: 26 Feb 2020 08:05

Re: How to get filename from URL and set it to variable?

#4 Post by Shohreh » 11 Aug 2020 10:43

Perfect! Thank you.

Post Reply