I have a text file of URLs, each in the form of:
http://domain.com/a/b/c/d.jpg
I want to download all of these, but save each file under the name:
c_d.jpg
In other words, for each file, I want to save the file under its original filename prefixed by the name of its parent directory.
How would I go about doing this on Windows?
I'm fine with using a command line tool such as wget or curl.
Thanks.
How to automatically save files with different names?
Moderator: DosItHelp
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Re: How to automatically save files with different names?
Something like this may help: put your URLS in url.txt
Code: Select all
@echo off
for /f "usebackq delims=" %%a in ("url.txt") do (
for /f "delims=" %%b in ("%%~pa\.") do (
echo "%%~nxb_%%~nxa"
echo wget %%a --output-document=%%~nxb_%%~nxa
)
)
pause
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Re: How to automatically save files with different names?
Works fine after removing the second echo prefix inside the loop. Thanks.
Breaking the URL up into sections was pretty puzzling to me. Can you recommend a source where I can learn about it?
Breaking the URL up into sections was pretty puzzling to me. Can you recommend a source where I can learn about it?
Re: How to automatically save files with different names?
transitionality wrote:Breaking the URL up into sections was pretty puzzling to me. Can you recommend a source where I can learn about it?
Ask focused questions of the community here and you will get tips and tricks.
Much of the batch coding you see around different places uses tricks and techniques that were developed over time and isn't necessarily documented.
The for /? will show you the for modifiers on the last screen, and which can be used to get the filename of a qualified path\filename.
This can also be used to get the last folder when the qualified path doesn't include a filename, and additionally in this case it also works when the path separator is a forward slash.
So the second for loop uses %%~p to isolate the URL path from the filename, and adding \. on the end is a way to make it work with a trailing slash.
When it is referenced with %%~nx it returns the last segment, which is normally the name and xtension, but is now the folder which contains the jpg file.
Re: How to automatically save files with different names?
What's really powerful about the batch foxidrive posted is that you can change the line: to and it will launch each download as its own process, downloading everything in parallel. This may be a bit for the server, so use with caution. You can always limit the bandwidth being used within wget.
Code: Select all
echo wget %%a --output-document=%%~nxb_%%~nxa
Code: Select all
echo start wget %%a --output-document=%%~nxb_%%~nxa