curl 命令输出带有时间戳的文件名

时间:2021-02-04 17:37:33

标签: curl cmd

我想从 url 获取 json 响应,并在 windows 命令提示符下使用 curl 将其保存到 .json 文件中。

我的目标是为文件名添加时间戳。

以下不起作用。

curl -k --ntlm -u : --write-out '%{json}' https://myurl.com -o "myfile$(date +\"%H:%M\").json"

它会生成一个没有内容的文件。 enter image description here

以下工作正常并产生预期的输出,但没有时间戳。

curl -k --ntlm -u : --write-out '%{json}' https://myurl.com -o "myfile.json"

如何获取带有时间戳的 .json 文件?

我在这里尝试了这两个建议,但似乎没有任何效果。 enter link description here

1 个答案:

答案 0 :(得分:1)

这应该有效:

curl -k --ntlm -u : --write-out '%{json}' https://myurl.com -o "myfile%TIME:~0,2%-%TIME:~3,2%.json"

%TIME% 是 Windows 内置的动态环境变量,~0,2~3,2 需要从中提取小时和分钟子字符串。

请注意,Windows 中的文件名中不允许使用冒号,因此我将其替换为破折号。