Page 1 of 1

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

Posted: 11 Aug 2020 05:27
by Shohreh
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.

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

Posted: 11 Aug 2020 06:33
by siberia-man
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

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

Posted: 11 Aug 2020 10:32
by aGerman
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

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

Posted: 11 Aug 2020 10:43
by Shohreh
Perfect! Thank you.