Search code examples
arraysfiledownloadautoit

Download a file reading first a file which contains the url


I have an mp3.txt file which contains URLs to download. How do I read the contents to then download the URLs? I tried this but it's not working:

Func _downloader($link)
    Local $dwnarrayread[3] = [IniRead(@ScriptDir & "\mp3.txt", "file_links", "link_1", Default), IniRead(@ScriptDir & "\mp3-2.txt", "file_links", "link_2", Default), IniRead(@ScriptDir & "\mp3-3.txt", "file_links", "link_3", Default)]
    $dwnlink = InetGet($dwnarrayread[$link], @ScriptDir & "PSU-04.mp3", 1, 1)

    Do
        Sleep(50)
        $prc = Round(InetGetInfo($dwnlink, 0) / (InetGetInfo($dwnlink, 1)) * 100)
        GUICtrlSetData($progressbar1, $prc)
    Until InetGetInfo($dwnlink, $INET_DOWNLOADCOMPLETE)
EndFunc

mp3.txt file :

[files_links]
link_1=https://ftp.psu.ac.th/pub/demo/mp3/PSU-04.mp3
link_2=http://somesite.com/files/file2.zip
link_3=http://somesite.com/files/file3.zip

Solution

  • How do I read the contents to then download the URLs?

    Example (as per additional requirement):

    _Download('mp3.txt', 'files_links', @DesktopDir & '\')
    
    Func _Download(Const $sFileIni, Const $sSection, Const $sDest)
        Local Const $aFile = IniReadSection($sFileIni, $sSection)
    
        For $i1 = 1 To $aFile[0][0]
    
            InetGet($aFile[$i1][1], $sDest & StringTrimLeft($aFile[$i1][1], StringInStr($aFile[$i1][1], '/', 0, -1)))
    
        Next
    
    EndFunc
    

    Example as per GUICtrlSetData() -attempt:

    #include <AutoItConstants.au3>
    #include <InetConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <MsgBoxConstants.au3>
    
    _Download('mp3.txt', 'files_links', @DesktopDir & '\')
    
    Func _Download(Const $sFileIni, Const $sSection, Const $sDest)
        Local Const $aFile = IniReadSection($sFileIni, $sSection)
        Local       $aDownload[$aFile[0][0] + 1]
    
        $aDownload[0] = $aFile[0][0]
    
        For $i1 = 1 To $aFile[0][0]
    
            $aDownload[$i1] = InetGet($aFile[$i1][1], $sDest & StringTrimLeft($aFile[$i1][1], StringInStr($aFile[$i1][1], '/', 0, -1)), $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
    
        Next
    
        Local Const $iWidth  = 400, _
                    $iHeight =  20
        Local $hWnd = GUICreate(@ScriptName, $iWidth, $iHeight * $aDownload[0])
        Local $aHnd[$aDownload[0] + 1]
    
        For $i1 = 1 To $aDownload[0]
    
            $aHnd[$i1] = GUICtrlCreateProgress(0, ($i1 - 1) * $iHeight, $iWidth, $iHeight)
            GUICtrlSetTip($aHnd[$i1], $aFile[$i1][1])
    
        Next
    
        GUISetState(@SW_SHOW, $hWnd)
        Local $aInfo
    
        While Not (GUIGetMsg() = $GUI_EVENT_CLOSE)
    
            For $i1 = 1 To $aDownload[0]
    
                $aInfo = InetGetInfo($aDownload[$i1])
                GUICtrlSetData($aHnd[$i1], ($aInfo[$INET_DOWNLOADCOMPLETE] Or $aInfo[$INET_DOWNLOADSUCCESS]) ? 100 : $aInfo[$INET_DOWNLOADSIZE] / $aInfo[$INET_DOWNLOADREAD])
    
            Next
    
            If InetGetInfo() Then ContinueLoop
    
            MsgBox($MB_OK, @ScriptName, 'No more running downloads.', 0, $hWnd)
            ExitLoop
    
        WEnd
    
        For $i1 = 1 To $aDownload[0]
    
            InetClose($aDownload[$i1])
    
        Next
    
    EndFunc