download a series of files automatically using command line/wget

5

2

I have a case that I would like to trigger an automatic download for a list of 114 file (recitation) for each reader,
for example if i want to download the recitations for a reader called abkr, the urls for the files will look like the following..

http://server6.mp3quran.net/abkr/001.mp3
http://server6.mp3quran.net/abkr/002.mp3
...
http://server6.mp3quran.net/abkr/113.mp3
http://server6.mp3quran.net/abkr/114.mp3

simply these are Quran recitations, so they are always have a total of 114

is there an easy way to loop that using command line on Windows ?

Anas Nakawa

Posted 2012-09-03T13:14:46.047

Reputation: 175

Which operating system? – Indrek – 2012-09-03T13:16:49.567

Windows is preferred, Mac is Ok as well.. – Anas Nakawa – 2012-09-03T14:18:54.207

Answers

5

For the sake of completeness, here's a batch-only solution:

@ECHO OFF
SetLocal EnableDelayedExpansion
FOR /L %%G IN (1, 1, 114) DO (
    SET num=%%G
    IF 1!num! LSS 100 SET num=0!num!
    IF 1!num! LSS 200 SET num=0!num!
    wget http://server6.mp3quran.net/abkr/!num!.mp3
)
EndLocal

Edit 1: Removed unnecessary braces.

Edit 2: Corrected counter start value to 1.

zb226

Posted 2012-09-03T13:14:46.047

Reputation: 493

all solutions are great, but I'm gonna accept this since it is straight forward (command-prompt), and does not require something other than wget.. – Anas Nakawa – 2012-09-05T05:31:41.550

a small correction, the loop starts from 1 instead of 0. – Anas Nakawa – 2012-09-05T05:32:11.037

Oops, correcting... – zb226 – 2012-09-05T08:09:25.697

4

You haven't stated OS, but if you are using *nix and Bash the following works:

wget http://server6.mp3quran.net/abkr/{001..114}.mp3

A solution that should work with any shell:

#!/bin/sh
for i in $(seq -w 1 114); do
    printf 'http://server6.mp3quran.net/abkr/%s.mp3 ' $i
done | xargs wget

or, if seq does not exist on the system:

#!/bin/sh
i=1
MAX=114
while [ $i -le $MAX ]; do
    printf 'http://server6.mp3quran.net/abkr/%03d.mp3 ' $i
    i=$((i+1))
done | xargs wget

Just copy+paste it in the shell or save it in a script file and run it.

Daniel Andersson

Posted 2012-09-03T13:14:46.047

Reputation: 20 465

is Mac OSX included in your *nix solution ? – Anas Nakawa – 2012-09-04T04:15:08.800

anasnakawa: Yes, you can run Bash on MacOSX, but it is not the default shell, I believe. Try just running the command "bash" in a terminal. You might need to install it beforehand, or look for a simple solution using the default shell. To clarify: the above will definitely work, but it might not be the most obvious way if Bash is not already installed. – Daniel Andersson – 2012-09-04T08:33:55.363

4

For a Windows solution, try the following PowerShell script:

$Client = New-Object System.Net.WebClient
for ($i = 1; $i -le 144; $i++)
{
    $file = [string]::Format("{0:D3}.mp3", $i)
    $Client.DownloadFile("http://server6.mp3quran.net/abkr/" + $file, $file)
}

First cd into the directory you want to download the files to, of course.

Indrek

Posted 2012-09-03T13:14:46.047

Reputation: 21 756

0

For reference and the sake of completeness a version without seq, a nice output and continue if the download stops for whatever reason:

#!/bin/sh
for i in 00{1..9} 0{10..99} {100..144}; do
    printf 'http://server6.mp3quran.net/abkr/%03d.mp3 ' $i
done | xargs wget -q --show-progress -c

HRitter

Posted 2012-09-03T13:14:46.047

Reputation: 1