找到字符串并调用不同的脚本

时间:2014-05-09 15:13:39

标签: batch-file

我正在尝试从文件中找到文本,如果找到字符串,则需要调用不同的批处理文件。当我尝试这个代码时没有任何反应。我不知道我哪里出错了。
这是代码。

@echo off
>nul find "BARNSLEY INTERCHANGE" location.txt && (
  call "barnsleyinterchange.bat"
) || (
   >nul find "DONCASTER INTERCHANGE" location.txt && (
      call "doncasterinterchange.bat"
   ) || (
      >nul find "CRYSTAL PEAKS" location.txt && (
         call "crystalpeaks.bat"
      ) || (
         >nul find "MIDDLEWOOD PARK RIDE" location.txt && (
            call "middlewoodpr.bat"
         ) || (
            >nul find "MEADOWHALL INTERCHANGE" location.txt && (
               call "meadowhallinterchange.bat"
            ) || (
               >nul find "ROTHERHAM INTERCHANGE" location.txt && (
                  call "rotherhaminterchange.bat"
               ) || (
                  >nul find "SHEFFIELD INTERCHANGE" location.txt && (
                     call "sheffieldinterchange.bat"
                  ) || (
                     echo "Syntax" was NOT found.
)))))))
pause

2 个答案:

答案 0 :(得分:1)

当你尝试@ThomasW的建议时(我同意他的观点,它的for想法会更好或者更容易维护),这是你的代码的替代方案,相同的想法,只是一点点简化

@echo off

    >nul find "BARNSLEY INTERCHANGE"   location.txt  && ( call "barnsleyinterchange.bat"    & goto out )
    >nul find "DONCASTER INTERCHANGE"  location.txt  && ( call "doncasterinterchange.bat"   & goto out )
    >nul find "CRYSTAL PEAKS"          location.txt  && ( call "crystalpeaks.bat"           & goto out )
    >nul find "MIDDLEWOOD PARK RIDE"   location.txt  && ( call "middlewoodpr.bat"           & goto out )
    >nul find "MEADOWHALL INTERCHANGE" location.txt  && ( call "meadowhallinterchange.bat"  & goto out )
    >nul find "ROTHERHAM INTERCHANGE"  location.txt  && ( call "rotherhaminterchange.bat"   & goto out )
    >nul find "SHEFFIELD INTERCHANGE"  location.txt  && ( call "sheffieldinterchange.bat"   & goto out )

    echo "Syntax" was NOT found.

:out
    pause

答案 1 :(得分:1)

无需外部文件即可使用FOR技术:

@echo off
for %%A in (
  "BARNSLEY INTERCHANGE|barnsleyinterchange.bat"
  "DONCASTER INTERCHANGE|doncasterinterchange.bat"
  "CRYSTAL PEAKS|crystalpeaks.bat"
  "MIDDLEWOOD PARK RIDE|middlewoodpr.bat"
  "MEADOWHALL INTERCHANGE|meadowhallinterchange.bat"
  "ROTHERHAM INTERCHANGE|rotherhaminterchange.bat"
  "SHEFFIELD INTERCHANGE|sheffieldinterchange.bat"
) do for /f "tokens=1,2 delims=|" %%B in (%%A) do (
  >nul find "%%~nB" location.txt && (
    call "%%C"
    goto :done
  )
)
echo "Syntax" was NOT found.

:done
pause

或者,如果您重命名批处理文件以匹配搜索字词,则:

@echo off
for %%A in (
  "BARNSLEY INTERCHANGE"
  "DONCASTER INTERCHANGE"
  "CRYSTAL PEAKS"
  "MIDDLEWOOD PARK RIDE"
  "MEADOWHALL INTERCHANGE"
  "ROTHERHAM INTERCHANGE"
  "SHEFFIELD INTERCHANGE"
) do >nul find "%%~A" location.txt && (
    call "%%~A.bat"
    goto :done
  )
)
echo "Syntax" was NOT found.

:done
pause