-3

I need to use cURL from the batch file (.bat) in Windows. My script is the following:

@echo off
curl "https://...url1..." -o "D:/path/file1.htm" "https://...url2..." -o "D:/path/file2.htm"

(here "https://...urlN..." represents a particular URL).

I have two questions:

  1. It seems that there is something wrong in the code: I only get two empty files in the output directory. In the command-line interface I see that cURL starts, but downloads zero bytes of data. Where is the problem? If I copy the command and paste it into the terminal, it works as expected.
  2. If I have multiple occurrences of "https://...urlN..." -o "D:/path/fileN.htm" for a single curl command, how to put all of them to separate lines (that is, each occurrence of "https://...urlN..." -o "D:/path/fileN.htm" must be placed in a separate line)?
8
  • Why must multiple curl commands be placed on the same line? Commented Nov 1 at 6:22
  • try curl "https://...url1..." -o "D:/path/file1.htm" & curl "https://...url2..." -o "D:/path/file2.htm" Commented Nov 1 at 6:23
  • @life888888: the curl ... & curl ... is not suitable in my situation; my question is about using a single curl command. Commented Nov 1 at 6:29
  • @life888888: "Why must multiple curl commands be placed on the same line?" — not multiple commands and not on the same line. I asked about consecutive parameters of the single command, placed on separate (multiple) lines. Why? For readability: each line would start with a particular URL, followed by the corresponding path and filename. Commented Nov 1 at 6:33
  • 2
    @lyricallywicked: you didn't name your script curl.bat, did you? Commented Nov 1 at 14:35

1 Answer 1

1

one curl command, use ^ at end of every line.

The ^ character must be the last character on a line, and there should be no spaces or other characters following it, followed by a newline.

  • create a batch file: test.bat
  • create a directory D:\DownloadWEB

test.bat

@echo off
curl "https://stackoverflow.com/questions" -o "D:\DownloadWEB\questions.html" ^
     "https://www.wikipedia.org/" -o "D:\DownloadWEB\wikipedia.html"  ^
     "https://us.yahoo.com" -o "D:\DownloadWEB\yahoo.html" ^
     --silent --show-error
  • open cmd.exe
  • run command: test.bat
  • check output file at directory D:\DownloadWEB

You can first test the test.bat file I provided.

If you confirm that there are no problems, you can gradually replace the URLs in test.bat and re-execute test.bat to test again.

Sign up to request clarification or add additional context in comments.

4 Comments

I do not know why, but it does not work. The CLI does not show any message, the output files are empty.
Attached is the content I used for testing, confirming that it can save files normally. The directory used for storage must already exist; if the directory does not exist, you must create it manually beforehand.
Thank you,, it works as expected if there is at least one space character in the beginning of each line after the ^ character. I have one additional question (to understand the syntax): what happens if there is no space character before the "https://... or --silent parts? How is the code interpreted in this situation?
This can be understood as follows: Your single-line command, in "https://..." or "--silent", already has spaces separating each parameter. Now, to enable line breaks, "^" is used to tell CMD.exe that a line break is needed. Therefore, the spaces before the original parameters should still be preserved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.