CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    14

    Help with multi-file downloader

    I want to create a program that can go into a parent directory, travel to each folder in the directory and then download each file from each folder. I'd also like to make it so that I can control the number of folders it downloads at a time, e.g. if there's 100 folders, then be able to tell it to download folders 1-10, then at another point tell it to download folders 11-20.
    This is the code that I have so far:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            WebClient wc = new WebClient();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                List<string> linkList = new List<string>();
                string url = textBox1.Text;
                string source = workerclass.getsource(url);
           
    
                int startindex = source.IndexOf("folder.gif");
                source = source.Substring(startindex, source.Length - startindex);
                while (source.IndexOf("<a href=") != -1)
                {
                    startindex = source.IndexOf("<a href=") + 9;
                    int endindex = source.IndexOf("/", startindex);
                    string link = source.Substring(startindex, endindex - startindex);
                    string fullink = url + link;
                    linkList.Add(fullink);
                    source = source.Replace("<a href=" + @"""" + link, "");
                }
                foreach (string link in linkList)
                {
    
                    List<string> linkList2 = new List<string>();
                    string newurl = textBox1.Text;
                    string source2 = workerclass.getsource(newurl);
    
    
                    int startindex2 = source.IndexOf("folder.gif");
                    source2 = source2.Substring(startindex, source2.Length - startindex2);
                    while (source2.IndexOf("<a href=") != -1)
                    {
                        startindex2 = source2.IndexOf("<a href=") + 9;
                        int endindex2 = source2.IndexOf("/", startindex);
                        string link2 = source.Substring(startindex2, endindex2 - startindex);
                        string fullink2 = newurl + link2;
                        linkList2.Add(fullink2);
                        source2 = source2.Replace("<a href=" + @"""" + link2, "");
                    }
                    foreach (string link2 in linkList2)
                    {
    
                        Uri reallink = new Uri(link2);
                        WebClient wc2 = new WebClient();
                        wc.DownloadFileAsync(reallink, link2+" file.zip");
    
                    }
                }
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    Would this work to grab each file in each folder and download it? How would I tell it how many/which folders to download at one time as I previously mentioned?

  2. #2
    Join Date
    Dec 2009
    Posts
    18

    Re: Help with multi-file downloader

    You want to be able to download file 1-10 at the same time so your downloading all 10 at once?

  3. #3
    Join Date
    Apr 2019
    Posts
    2

    Re: Help with multi-file downloader

    This solution works across browsers, and does not trigger warnings. Rather than creating an iframe, here we creates a link for each file. This prevents mega warning messages from popping up.

    Code:
    /**
     * Download a list of files.
     * @author speedplane
     */
    function download_files(files) {
      function download_next(i) {
        if (i >= files.length) {
          return;
        }
        var a = document.createElement('a');
        a.href = files[i].download;
        a.target = '_parent';
        // Use a.download if available, it prevents plugins from opening.
        if ('download' in a) {
          a.download = files[i].filename;
        }
        // Add a to the doc for click to work.
        (document.body || document.documentElement).appendChild(a);
        if (a.click) {
          a.click(); // The click method is supported by most browsers.
        } else {
          $(a).click(); // Backup using jquery
        }
        // Delete the temporary link.
        a.parentNode.removeChild(a);
        // Download the next file with a small timeout. The timeout is necessary
        // for IE, which will otherwise only download the first file.
        setTimeout(function() {
          download_next(i + 1);
        }, 500);
      }
      // Initiate the first download.
      download_next(0);
    }
    <script>
      // Here's a live example that downloads three test text files:
      function do_dl() {
        download_files([
          { download: "http://www.nt.az/reg.txt", filename: "regs.txt" },
          { download: "https://www.w3.org/TR/PNG/iso_8859-1.txt", filename: "standards.txt" },
          { download: "http://qiime.org/_static/Examples/File_Formats/Example_Mapping_File.txt", filename: "example.txt" },
        ]);
      };
    </script>
    <button onclick="do_dl();">Test downloading 3 text files.</button>
    Last edited by officially4h; January 25th, 2020 at 12:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured