bat文件中else语句中无法识别的变量

bat文件中else语句中无法识别的变量

本文介绍了bat文件中else语句中无法识别的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

ECHO OFF
:LOOP
set c=
setlocal ENABLEDELAYEDEXPANSION
tasklist | find /i "calc.exe" >nul 2>&1
IF %ERRORLEVEL% == 0 (
  ECHO easymeetingOnCall is running
  taskkill /IM calc.exe /F
  set c=true
  Timeout /T 10
  goto LOOP
) ELSE (
  IF "!c!" == "true" (
    ECHO l'applicazione easymeetingOnCall è spenta.
    Timeout /T 10
    goto exitLoop
  )
  ECHO easymeetingOnCall is not running
  Timeout /T 5 /Nobreak
  GOTO LOOP
)
:exitLoop

我的问题是遵循条件

IF "!c!" == "true"

不识别进入else语句.如果我写 echo!c!,它不会输出任何结果.我之前设置了enabledelayedexpansion ,所以我不知道为什么会这样.

Into the else statement, is not recognized.If I write echo !c! It doesn't output any result.I set enabledelayedexpansion before, so I don't know why of this behavior.

你能帮我吗?谢谢大家.

Could you help me?Thanks to all.

推荐答案

仔细查看代码中的以下摘录.

Look carefully at the following extraction from your code.

ECHO OFF
:LOOP
set c=
....
set c=true
goto :LOOP

您要显式地将if语句告知 goto:loop ,在您直接对 set c = 进行设置之后,该位置现在没有任何值.将 set c = 移至上方以标记以保留您设置的值.

You are explicitly telling the if statement to goto :loop where directly after you set c= which now gives it no value. Move the set c= to above to label to retain the value you've set.

但是,我建议进行一些更改.您可以在没有 delayedexpansion 的情况下摆脱困境,并确实需要所有的for循环.使用 taskkill 显式搜索该应用程序比列出所有列表更好,这在@Mofi的评论中也已提及.最后,设置单个字符变量并不是一个很好的习惯,尽管它并不总是会引起问题,我建议使用多个字符变量名.我只是将变量%c%更改为%_ c%

I however suggest a few changes. you can get away without delayedexpansion and do actually require all the for loops. Using taskkill to explicitly search for the app is better than listing all, also already mentioned to you in a comment by @Mofi. Lastly, it is not really good practice to set single character variables, though it does not always cause issues, I would suggest using multple character variable names. I just changed your variable %c% to %_c%

@echo off & set _c=
:loop
tasklist /FI "IMAGENAME eq Calc.exe" | find /i "Calc"
goto :_%errorlevel%
:_0
  ECHO easymeetingOnCall is running
  taskkill /IM calc.exe /F
  set _c=true
  Timeout /T 10
  goto loop
:_1
  IF "%_c%" == "true" (
    ECHO l'applicazione easymeetingOnCall è spenta.
    Timeout /T 10
    goto exitloop
  )
  ECHO easymeetingOnCall is not running
  Timeout /T 5 /Nobreak
  goto loop
  :exitloop

编辑,因为您希望使用列表:

Edit, as you wanted to do this with a list:

@echo off & set _c=
set "list=calc.exe StickyNot.exe wordpad.exe"
for %%a in (%list%) do call :loop %%a
goto :eof
:loop
set task=%1
 tasklist /FI "IMAGENAME eq %1" | find /i "%1"
   if not errorlevel 1 (
   ECHO easymeetingOnCall is running %1
   taskkill /IM %1 /F
   set _c=true
   Timeout /T 10
)
if "%_c%" == "true" (
    ECHO l'applicazione easymeetingOnCall è spenta. %1
    Timeout /T 10
    goto exitloop
  )
    echo easymeetingOnCall is not running %1
    Timeout /T 5 /Nobreak
    goto :loop
  :exitloop

请注意,如果第一个进程未运行,它将永远循环直到找到为止,然后仅将其杀死并转到下一个进程.如果您不希望这样做,请在最后一行更改 goto:loop 以退出循环.

Be aware, if the first process is not running, it will loop forever until it is found, then only kill it and go to the next process. If you do not want that, then change goto :loop in the last line to exit loop.

这篇关于bat文件中else语句中无法识别的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:03