How to automatically save files with different names?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

How to automatically save files with different names?

#1 Post by transitionality » 09 Jul 2014 03:58

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to automatically save files with different names?

#2 Post by foxidrive » 09 Jul 2014 04:24

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

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: How to automatically save files with different names?

#3 Post by transitionality » 09 Jul 2014 05:57

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to automatically save files with different names?

#4 Post by foxidrive » 09 Jul 2014 06:19

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.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: How to automatically save files with different names?

#5 Post by Samir » 10 Jul 2014 23:29

What's really powerful about the batch foxidrive posted is that you can change the line:

Code: Select all

echo wget %%a --output-document=%%~nxb_%%~nxa
to

Code: Select all

echo start wget %%a --output-document=%%~nxb_%%~nxa
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.

Post Reply