Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

Write me code for a batch script that asks for input for a direct download link…

  1. User

    user: Write me code for a batch script that asks for input for a direct download link and uses curl to download it to your computer, specifically in %temp% and then it opens it if its an executable ,or msi or any other f extension.

    it also curls silently and also make a custom like message for how much is left to be downloaded and how much has been downloaded PROMPT: user: Write me code for a batch script that asks for input for a direct download link and uses curl to download it to your computer, specifically in %temp% and then it opens it if its an executable ,or msi or any other f extension.

    it also curls silently and also make a custom like message for how much is left to be downloaded and how much has been downloaded

  2. Assistant

    You can achieve the desired functionality using a batch script that leverages curl for downloading and some Windows command-line utilities for basic logic. Here's an example script:

    @echo off
    setlocal
    
    rem Ask for the download URL
    set /p downloadUrl=Enter the direct download link: 
    
    rem Set the download destination
    set destination=%temp%\downloaded_file
    
    rem Download the file using curl
    echo Downloading file, please wait...
    curl -L -o "%destination%" -s --progress-bar --write-out "Total size (bytes): %{size_download}\n" "%downloadUrl%" | (set /p value= && echo %value%)
    if errorlevel 1 (
        echo Failed to download the file.
        exit /b 1
    )
    
    rem Get the file extension
    for %%F in ("%destination%") do set "extension=%%~xF"
    
    rem Check if the file is an executable or an installer
    if /i "%extension%"==".exe" (
        echo Opening the executable file...
        start "" "%destination%"
    ) else if /i "%extension%"==".msi" (
        echo Opening the MSI installer...
        start "" "%destination%"
    ) else (
        echo File downloaded successfully, but it is not an executable or MSI.
    )
    
    endlocal
    exit /b 0
    

    How it works:

    1. User Input: It prompts the user to enter a direct download link.
    2. Download Location: The file is saved to the %temp% directory with a temporary name.
    3. curl Usage: Uses curl to silently download the file while also displaying progress with --progress-bar. The --write-out option is used to display the total size in bytes after download.
    4. File Type Check and Execution:
      • It checks the file's extension to determine if it's an executable or MSI file.
      • If it's an executable (.exe) or MSI (.msi), it will automatically open/run the file.
      • If not, it notifies the user that the download is complete but does not open the file.

    Note:

    • Privileges: Running executables/MSI files may require administrative privileges, depending on the system's settings.
    • Error Handling: Basic error handling is implemented to notify the user if the download fails.
    • Extension Handling: You can expand the script to handle more file types by adding more conditions in the extension check section.