【问题标题】:Can't remove a part of string batch无法删除字符串批处理的一部分
【发布时间】:2016-05-30 15:06:32
【问题描述】:

我创建了一个批处理文件来运行test并运行test1如果它还没有运行 但是当我运行它时

  @echo off

start test.exe  >nul 2>nul

tasklist /fi "IMAGENAME eq test1.exe">tmp.t
set /p "isRunning=<tmp.t"
%isRunning%>test1.txt
pause
IF NOT /i %isRunning:INFO=%==%isRunning% (start X:\X\test1.exe >nul 2>nul)
timeout 10
del tmp.t

它说

'INFO:' Is not regonized as an internal or external command,operable program
or batch file 

然后

:was unexpected at the time

test1.txt 也是空的。这条线IF NOT /i %isRunning:~INFO=%==%isRunning% 被要求检查进程是否正在运行。它似乎 像字符串替换没有按应有的方式工作%isRunning:INFO=%

我不知道出了什么问题。

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    最好使用&amp;&amp;|| 运算符:

    @echo off
    Set "MyProcess=test1.exe"
    
    tasklist | find /i "%MyProcess%">nul  && echo %MyProcess% Is running || start "" "X:\X\test1.exe" >nul
    

    来自this post

    只是回答(部分)您的问题:

    /i 参数必须直接放在if 语句之后。

    如果字符串为空,则在测试变量之间放置双引号可避免错误:

    IF /i NOT "%isRunning:INFO=%"=="%isRunning%" (start X:\X\test1.exe >nul 2>nul)
    

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 2011-03-14
      • 2012-03-31
      • 2017-05-11
      • 2018-12-07
      • 1970-01-01
      相关资源
      最近更新 更多