gpt4 book ai didi

batch-file - 具有嵌套循环的批处理文件中的范围问题

转载 作者:行者123 更新时间:2023-12-03 11:22:22 26 4
gpt4 key购买 nike

我有一个 SSIS 作业在异步执行的批处理文件中运行。

我需要知道 SSIS 作业何时完成输出一堆 PDF 和 XLS 文件。

文件出现在两个目录中,首先是 PDF,然后是 XLS。

我选择写入第二个批处理文件,SSIS 作业退出后会稍等片刻,然后检查目录中写入的最后一个文件是否已存在 3 分钟,经过观察,这是一个充足的时间间隔用于写入文件的作业。

问题是:如果内循环迭代不止一次,外循环永远不会运行,这似乎表明第二次 MYPATH已声明,n 的值是 foobarred,但这不可能是真的,因为脚本返回 1,而不是在检查 Arr[badval] 时崩溃。

@ECHO OFF

REM SSIS process is asyncronous, and executes in background. Problematic for
REM DAG, which relies on exit code to understand process.
REM Check for "last file written" in output directories every N seconds.
REM It's a good bet we are done when they match.

@setlocal enabledelayedexpansion

REM "sleep" wait for non-existent command to complete
waitfor ragnarok /t 180>NUL 2>&1

set Arr[0]=E:\pdf_output
set Arr[1]=E:\xls_output

for /l %%n in (0,1,2) do (

if defined Arr[%%n] (
REM set value for path within loop or scope will bite you
set MYPATH=!Arr[%%n]!
echo Checking file ages in !MYPATH!.
) else (
echo Done
EXIT /B 0
)

:while1
REM don't put a blank line here, it throws a syntax error
FOR /F "delims=|" %%I IN ('DIR !MYPATH! /B /O:D') DO SET FILE1=%%I

REM "sleep" use ping for delay, since waitfor will break loop
arp -s 192.168.1.254 >nul
ipconfig /flushdns >nul
ping localhost -n 180 >nul

FOR /F "delims=|" %%I IN ('DIR !MYPATH! /B /O:D') DO SET FILE2=%%I

if NOT "!FILE1!" == "!FILE2!" (
goto :while1
)
)
endlocal
REM Something is wrong, return 1 to stop DAG and invite inspection.
exit /B 1

最佳答案

按照@aschipfl 的建议将 while 循环分解成一个子例程似乎已经成功了:

@ECHO OFF

REM SSIS process is asyncronous, and executes in background. Problematic for
REM DAG, which relies on exit code to understand process.
REM Check for "last file written" in output directories every N seconds.
REM It's a good bet we are done when they match.

@setlocal enabledelayedexpansion

REM "sleep" wait for non-existent command to complete
waitfor ragnarok /t 120>NUL 2>&1

set Arr[0]=E:\pdf_output
set Arr[1]=E:\xls_output

for /l %%n in (0,1,2) do (

if defined Arr[%%n] (
REM set value for path within loop or scope will bite you
set MYPATH=!Arr[%%n]!
echo Checking file ages in !MYPATH!.
CALL :checkfiles
) else (
echo Done
goto :NormalExit
)
)

:checkfiles
:while1
REM don't put a blank line here, it throws a syntax error
FOR /F "delims=|" %%I IN ('DIR !MYPATH! /B /O:D') DO SET FILE1=%%I

REM "sleep" use ping for delay, since waitfor will break loop
arp -s 192.168.1.254 >nul
ipconfig /flushdns >nul
ping localhost -n 120 >nul

FOR /F "delims=|" %%I IN ('DIR !MYPATH! /B /O:D') DO SET FILE2=%%I

if NOT "!FILE1!" == "!FILE2!" (
goto :while1
)
endlocal

:NormalExit
exit /B 0

REM Something is wrong, return 1 to stop DAG and invite inspection.
exit /B 1

关于batch-file - 具有嵌套循环的批处理文件中的范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com