How to Download File From URL in Batch Script

  1. Using Curl Command
  2. Downloading Multiple Files
  3. Error Handling in Batch Scripts
  4. Conclusion
  5. FAQ
How to Download File From URL in Batch Script

In today’s digital age, downloading files from URLs using scripts has become a common task for developers and system administrators alike. Batch scripts, in particular, provide a simple yet powerful way to automate this process on Windows systems. Whether you’re looking to download a single file or multiple files from various URLs, mastering batch scripts can save you time and effort.

In this tutorial, we will explore how to download files from URLs using batch scripts, focusing on both the basic commands and some advanced techniques. By the end of this guide, you’ll be equipped with the knowledge to streamline your file downloading tasks effectively.

Batch scripts are text files containing a sequence of commands that can be executed by the command-line interpreter on Windows. They are particularly useful for automating repetitive tasks, like downloading files from the internet. The beauty of batch scripts lies in their simplicity; you can write them using any text editor and execute them directly from the command prompt.

To get started, you will need to open a text editor like Notepad and begin writing your batch script. The most common command used for downloading files in a batch script is curl, which is included in Windows 10 and later versions. If you’re using an older version of Windows, you may need to install it separately.

Using Curl Command

The curl command is a powerful tool for transferring data from or to a server using various protocols, including HTTP, HTTPS, and FTP. Downloading a file using curl in a batch script is straightforward. Here’s how you can do it:

@echo off
set URL=https://example.com/file.zip
set OUTPUT=file.zip
curl -o %OUTPUT% %URL%

Output:

file.zip                     100%[====================================>]  1.23M  1.23MB/s    in 0.9s    

In this script, we start by turning off the command echoing with @echo off. Then, we set the URL variable to the file you want to download and the OUTPUT variable to the name you want to give the downloaded file. The curl command uses the -o option to specify the output file name, followed by the URL of the file to be downloaded. When you run this script, it will download the specified file from the URL and save it with the given name.

Using curl is not only efficient but also allows for various options, such as resuming interrupted downloads or specifying user authentication.

Downloading Multiple Files

If you need to download multiple files from different URLs, you can easily extend your batch script. Here’s an example of how to do that:

@echo off
setlocal enabledelayedexpansion

set URLs=https://example.com/file1.zip https://example.com/file2.zip https://example.com/file3.zip
set OUTPUT_DIR=downloads

mkdir %OUTPUT_DIR%

for %%u in (%URLs%) do (
    set FILENAME=%%~nxu
    curl -o "%OUTPUT_DIR%\!FILENAME!" %%u
)

Output:

file1.zip                   100%[====================================>]  1.23M  1.23MB/s    in 0.9s    
file2.zip                   100%[====================================>]  1.45M  1.45MB/s    in 1.0s    
file3.zip                   100%[====================================>]  1.67M  1.67MB/s    in 1.1s    

In this script, we define a list of URLs separated by spaces and specify an output directory where the files will be saved. The mkdir command creates the directory if it doesn’t already exist. The for loop iterates through each URL, extracting the filename and using curl to download the file into the specified directory. The use of enabledelayedexpansion allows us to correctly handle variable expansion within the loop.

This method is efficient for downloading multiple files in one go, saving you the hassle of executing separate commands for each file.

Error Handling in Batch Scripts

When downloading files, it is essential to handle potential errors gracefully. Here’s how you can implement basic error handling in your batch script:

@echo off
set URL=https://example.com/file.zip
set OUTPUT=file.zip

curl -o %OUTPUT% %URL%
if errorlevel 1 (
    echo Download failed. Please check the URL or your internet connection.
) else (
    echo Download completed successfully: %OUTPUT%
)

Output:

Download completed successfully: file.zip

In this script, we check the exit status of the curl command using if errorlevel 1. If the command fails (returns a non-zero exit code), we print an error message. Otherwise, we confirm that the download was successful. This simple error handling mechanism can help you troubleshoot issues when a download does not go as planned.

Conclusion

Downloading files from URLs using batch scripts is a skill that can significantly enhance your productivity. By leveraging commands like curl, you can automate the process for both single and multiple files, while also incorporating error handling to ensure a smooth experience. Whether you are managing downloads for work or personal projects, mastering batch scripts will empower you to handle file downloads efficiently.

With the techniques outlined in this tutorial, you’re now ready to put your batch scripting skills to the test. Start creating your scripts today and simplify your file downloading tasks!

FAQ

  1. How do I create a batch script?
    You can create a batch script by opening a text editor, writing your commands, and saving the file with a .bat extension.

  2. Can I use batch scripts on older versions of Windows?
    Yes, batch scripts are supported on older versions of Windows, but you may need to install commands like curl separately.

  3. What if I encounter errors while downloading?
    You can implement error handling in your batch script to check the exit status of commands and provide feedback.

  4. Is it possible to download files using FTP in batch scripts?
    Yes, you can use curl or other FTP commands in batch scripts to download files from FTP servers.

  5. Can I schedule batch scripts to run automatically?
    Yes, you can use Windows Task Scheduler to run your batch scripts at specified times or intervals.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Batch Script