Does anyone have a quick and easy piece of code that I could use to point to the url location of a bunch of image files and download them to a local folder? For example, imagine I wanted to download through .../picture999onsite.jpg.

I've not had a lot of experience in C++ and would greatly appreciate any help you can provide in getting started.

Thanks so much.
- R

Dani AI

Generated

Following 's description and the early replies from and , here are compact, practical examples and tips for downloading a sequence of JPGs. Each snippet shows zero-padded numbering, basic error handling (remove partial files on HTTP errors), and a small delay to be polite to the server. Test a small range first.

C++ (libcurl):

#include <cstdio>
#include <curl/curl.h>

int main() {
  curl_global_init(CURL_GLOBAL_DEFAULT);
  for (int i=1; i<=999; ++i) {
    char url[256], out[64];
    snprintf(url, sizeof(url), "http://example.com/images/picture%04d.jpg", i);
    snprintf(out, sizeof(out), "picture%04d.jpg", i);
    FILE* f = fopen(out, "wb");
    if (!f) continue;
    CURL* c = curl_easy_init();
    if (c) {
      curl_easy_setopt(c, CURLOPT_URL, url);
      curl_easy_setopt(c, CURLOPT_WRITEDATA, f);
      curl_easy_setopt(c, CURLOPT_FAILONERROR, 1L);
      CURLcode res = curl_easy_perform(c);
      curl_easy_cleanup(c);
      fclose(f);
      if (res != CURLE_OK) remove(out);
    } else { fclose(f); }
    sleep(1);
  }
  curl_global_cleanup();
  return 0;
}

Ruby (open-uri) and PowerShell (.NET WebClient):

# Ruby
require 'open-uri'
(1..999).each do |i|
  name = format('%04d', i)
  begin
    open("http://example.com/images/picture#{name}.jpg") do |u|
      File.open("picture#{name}.jpg","wb"){|f| f.write(u.read) }
    end
  rescue OpenURI::HTTPError; end
  sleep 0.2
end
# PowerShell
$wc = New-Object System.Net.WebClient
for ($i=1;$i -le 999;$i++){
  $n=$i.ToString('D4'); $u="http://example.com/images/picture${n}.jpg"; $out="picture${n}.jpg"
  try { $wc.DownloadFile($u,$out) } catch { Remove-Item $out -ErrorAction SilentlyContinue }
  Start-Sleep -Milliseconds 200
}

Operational tips: respect site terms/robots, add delays or rate limits, set a sensible User-Agent/Referer if required, log failures, implement retries/backoff, and avoid high-concurrency brute-force hits. On Windows, libcurl needs a linked binary (or use the PowerShell or Ruby options for quicker setup).

Recommended Answers

All 4 Replies

What operating system?
Are you required to use C++ ? I can think of a few easier ways to do this.

Not tied to C++. I know it and Java, just learning ruby. Thought it'd be an interesting thing to learn in one of those contexts, but bottom line is I do need to get the files down (too many to do manually) to my HD, so any suggestion you can offer would be greatly appreciated.

Thanks,
- Ron

Oh, sorry, forgot to answer your first question. I've got Windows 7 Pro. Have access to NetBeans, Aptana, or Visual Studio 2008.

Not tied to C++. I know it and Java, just learning ruby. Thought it'd be an interesting thing to learn in one of those contexts, but bottom line is I do need to get the files down (too many to do manually) to my HD, so any suggestion you can offer would be greatly appreciated.

Thanks,
- Ron

In that case, use wget. No programming required whatsoever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.