在W10批处理文件中,在块中时无法正确评估'if errorlevel'

时间:2019-06-10 17:20:21

标签: batch-file block errorlevel

当IF位于for块中时,我的错误级别始终保持为0的问题

我尝试了不同的语法%!等

@Echo Off
rem no correct evaluation of IF ERRORLEVEL (syntax % or !)
setlocal enabledelayedexpansion

For /F Tokens^=6Delims^=^" %%A In ('WMIC Path Win32_ShortcutFile Where^
 "Extension='lnk' And Name Like '%AppData:\=\\%\\Microsoft\\Windows\\Recent\\%%'"^
 Get Target /Format:"MOF" 2^>Nul')Do For %%B In (odt pdf txt doc docx wpd wps csv swd vor uot uof pdb psw xls xlsx xlm wk1 wks 123 dif rtf sdc vor dbf slkppt pps pptx 
)Do If /I "%%~xA"==".%%B" If Exist "%%A" (
Echo(%%A
MailAlert -s "my PC MEDION" -r first.last@yahoo.be -b "recente" -a "%%A"

IF '!errorlevel!'=='0' (
    rem the IF is not executed ; neither with '%errorlevel%'=='0'
    rem   '%errorlevel%'=='0'   was tested succesfully  outside the for block !!
    echo isSend: %%A
    rem shortcut to be deleted 
) ELSE (
    ECHO "An error was found w/ error code of: %ERRORLEVEL%"
        rem  !! errorlevel was never correct (is always 0) although the if function was correctly evaluated (as beeiing not 0) but only outside the 'for' block !!! 
    ECHO "in file:%%A" 
    rem shortcut not to be deleted 
) 

)



在for块之外,我通过发送一个打开的附件文件测试了代码,这引发了一个错误(由mailalert产生),如果'%errorlevel%==…可以很好地检测到该错误。.另一个未打开的文件是由mailalert有效发送的,并引发了错误级别0。

1 个答案:

答案 0 :(得分:0)

<WebView
    ref={webView => { this.refWeb = webView; }}
    javaScriptEnabled={true}
    injectedJavaScript={myInjectedJs}
    ...

这显示了如何将@echo off setlocal enabledelayedexpansion rem Following 4 lines are the powershell command set pscommand=^&{^ $sh= New-Object -ComObject WScript.Shell;^ ForEach ($lnk in Get-ChildItem '!AppData!\Microsoft\Windows\Recent\*.lnk'^) {^ ForEach ($target in $sh.CreateShortcut($lnk^).TargetPath^) {Write-Host $lnk'^|'$target}}} for /f "tokens=1-2 delims=|" %%A in ('powershell -noprofile -command "!pscommand!"') do ( rem Disable delayed expansion as can remove exclaimation marks in paths. setlocal disabledelayedexpansion for %%C In ( odt pdf txt doc docx wpd wps csv swd vor uot uof pdb psw xls xlsx xlm wk1 wks 123 dif rtf sdc vor dbf slkppt pps pptx ) do ( if /i "%%~xB" == ".%%~C" if exist "%%~B" ( echo(Link: "%%~A"& echo(Target: "%%~B" rem Attach .lnk file to mail MailAlert -s "my PC MEDION" -r first.last@yahoo.be -b "recente" -a "%%~A" if errorlevel 1 ( rem Shortcut not to be deleted rem Backup errorlevel using call set to resolve the doubled percent signs. call set "error_level=%%errorlevel%%" rem Enable delayed expansion to echo the backup of errorlevel setlocal enabledelayedexpansion echo An error was found w/ error code of: !error_level! endlocal echo inFile: "%%~B" ) else ( rem Shortcut to be deleted echo del "%%~A" echo isSend: "%%~B" ) echo( ) ) endlocal ) 备份到另一个 变量,并将errorlevel设置为 setlocal可以设置setlocal

除非errorlevel中的echo很重要, 我会忽略它,因为它是唯一的命名变量 需要在errorlevel循环中进行扩展。 没有它,就需要扩展 启用延迟扩展或for,或 不需要call set。代码 仅显示call echo的1个变体 echo

路径可能带有感叹号,因此启用延迟 路径扩展为文本时可以扩展 使路径中的惊叹号消失, 从而使受影响的路径无效。为了避免这种情况, 根据需要启用和禁用延迟扩展。

我发现errorlevel相当慢,因为它在整个系统范围内 搜索,但您的代码未显示任何结果,因此 正在使用wmic从中读取目标 powershell个文件。 如果适用,它可以代替.lnk

wmic变量:

  • for是链接路径
  • %%A是目标路径
  • %%B是要与目标路径扩展进行比较的扩展

查看%%C,以获取有关处理if /?的信息。 请注意,errorlevel检查不需要 变量展开。