windows - 发生错误时循环的批处理脚本停止

标签 windows for-loop batch-file curl

我有一个批处理脚本片段,应该对文件夹中的所有文件进行迭代,然后使用curl将它们发送到API。我面临的问题是在迭代过程中,如果一个文件在curl操作过程中产生一些错误,则循环停止(其余文件不会被 curl (如果这是一个技术性的词)。我需要一种方法,如果对文件的curl操作失败,则为循环继续其他文件
以下是我正在使用的代码-

for /R "%SourceDirectory%" %%I in (*.csv) do (
    curl --data-binary @%%~fI  -H "Content-Type: text/csv" -H "Authorization: Bearer "%secrettoken%"""" %myAPI% >> "%LogFilePath%"
)

最佳答案

使用 for /f + where /r 代替 for /r

@echo off && for /f tokens^=* %%I in (%__APPDIR__%where.exe /r "%SourceDirectory%" *.csv')do >>"%LogFilePath%" (
curl --data-binary @"%%~I" -H "Content-Type: text/csv" -H "Authorization: Bearer" "%secrettoken%" "%myAPI%" )
  • where /?
  • 关于windows - 发生错误时循环的批处理脚本停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59934087/

    相关文章:

    file - 如何比使用命令 COPY 并指定所有文件名更有效地将文件 1 到 X 连接到新文件?

    windows - 如何在 CMD/Batch 中将数字拆分为单个数字

    windows - 在 Windows 上的 Git Bash 中设置 Spark-shell

    c++ - 微软 Visual Studio : Windows and Unix project source code compatibility

    c - "Trapping"Windows用户空间进程自己的sysenter调用

    c++ - VC++ 6.0 - WinDbg 堆栈跟踪显示 malloc 的崩溃点

    loops - For 和 While 循环 : differences, 问题及原因

    batch-file - 我如何让 Else 在 Batch 中工作?

    c - 函数内的数组

    Python while 和 for 循环 - 我可以使代码更高效吗?