代码之家  ›  专栏  ›  技术社区  ›  Clint Hartley

你能把goto和if放在一行吗?

  •  1
  • Clint Hartley  · 技术社区  · 7 年前

    我试图将goto和if-then命令放在同一行中,因为这样我就不必再制作一个BAT文件来完成工作。 下面是我目前正在编写的代码示例:

    @echo off
    SET _cliportaskkill=0
    SET _startffox=0
    
    If "%_cliportaskkill%"=="2" goto everything
    If "%_cliportaskkill%"=="1" goto cliponly
    If "%_cliportaskkill%"=="0" goto taskkillonly
    
    
    :cliponly
    echo|set /p=username@email.com|clip
    EXIT
    
    :taskkillonly
    TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe
    EXIT
    
    :everything
    echo|set /p=username@email.com|clip
    
    TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe
    EXIT
    
    ===================================================================================================================
    
    @echo off
    If "%_startffox%"=="1" goto newwindow
    If "%_startffox%"=="0" goto newtab
    
    :newwindow
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/accounts/username/recently-played"
    EXIT
    
    :newtab
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe -new-tab "https://www.outlook.com/" -new-tab "http://everybodyedits.com/" -new-tab "http://www.kongregate.com/accounts/username/recently-played"
    EXIT
    

    Whcih用于此BAT文件: 老维森

    @echo on
    START "taskkilling and cliping" CMD /c "@echo off & echo|set /p=username@email.com|clip & TASKKILL /F /T /IM Greenshot.exe & TASKKILL /F /T /IM iprntctl.exe & TASKKILL /F /T /IM iprntlgn.exe & TASKKILL /F /T /IM smax4pnp.exe & TASKKILL /F /T /IM ZenNotifyIcon.exe & TASKKILL /F /T /IM ZenUserDaemon.exe & TASKKILL /F /T /IM RAVBg64.exe & TASKKILL /F /T /IM nwtray.exe & TASKKILL /F /T /IM RtkNGUI64.exe & TASKKILL /F /T /IM BingSvc.exe & TASKKILL /F /T /IM Spotify.exe & TASKKILL /F /T /IM SpotifyWebHelper.exe & TASKKILL /F /T /IM FacebookGameroom.exe & TASKKILL /F /T /IM "Facebook Gameroom Browser.exe" & TASKKILL /F /T /IM zapp.exe & TASKKILL /F /T /IM steamwebhelper.exe & TASKKILL /F /T /IM Steam.exe & TASKKILL /F /T /IM lync.exe"
    
    START "start new firefox in three new tabs" CMD /c "@echo off & CD /D "C:\Program Files (x86)\Mozilla Firefox" & firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/accounts/username/recently-played""
    

    因此,我正在尝试制作一个BAT文件,其中有一些选项,我可以在登录到计算机时用于我的新版本。 到目前为止,我尝试通过另一个BAT文件测试其中一个代码,所以这里是我到目前为止得到的:

    SET _startffox=0
    CHOICE /C wt /m "w open new window, t open new tab"
    IF errorlevel 2 set _startffox=0 & goto line
    IF errorlevel 1 set _startffox=1 & goto line
    
    :line
    pause & echo %_startffox%
    (echo on & (If "%_startffox%"=="1" goto newwindow); (If "%_startffox%"=="0" goto newtab); (:newwindow & ECHO "go to new window firefox" & ECHO "Hi windows" & pause & EXIT); (:newtab & ECHO "go to new tabs firefox" & ECHO "Hi tabs" & pause & EXIT))
    pause
    

    到目前为止,它还没有像我想的那样工作。

    2 回复  |  直到 7 年前
        1
  •  0
  •   michael_heath    7 年前
    @echo off
    setlocal
    
    SET "_cliportaskkill=0"
    SET "_startffox=0"
    
    If "%_cliportaskkill%"=="2" call :everything
    If "%_cliportaskkill%"=="1" call :cliponly
    If "%_cliportaskkill%"=="0" call :taskkillonly
    
    If "%_startffox%"=="1" call :newwindow
    If "%_startffox%"=="0" call :newtab
    EXIT /B
    
    
    :cliponly
    echo|set /p=username@email.com|clip
    EXIT /B
    
    
    :taskkillonly
    TASKKILL /F /T^
     /IM Greenshot.exe^
     /IM iprntctl.exe^
     /IM iprntlgn.exe^
     /IM smax4pnp.exe^
     /IM ZenNotifyIcon.exe^
     /IM ZenUserDaemon.exe^
     /IM RAVBg64.exe^
     /IM nwtray.exe^
     /IM RtkNGUI64.exe^
     /IM BingSvc.exe^
     /IM Spotify.exe^
     /IM SpotifyWebHelper.exe^
     /IM FacebookGameroom.exe^
     /IM "Facebook Gameroom Browser.exe"^
     /IM zapp.exe^
     /IM steamwebhelper.exe^
     /IM Steam.exe^
     /IM lync.exe
    EXIT /B
    
    
    :everything
    echo|set /p=username@email.com|clip
    
    TASKKILL /F /T^
     /IM Greenshot.exe^
     /IM iprntctl.exe^
     /IM iprntlgn.exe^
     /IM smax4pnp.exe^
     /IM ZenNotifyIcon.exe^
     /IM ZenUserDaemon.exe^
     /IM RAVBg64.exe^
     /IM nwtray.exe^
     /IM RtkNGUI64.exe^
     /IM BingSvc.exe^
     /IM Spotify.exe^
     /IM SpotifyWebHelper.exe^
     /IM FacebookGameroom.exe^
     /IM "Facebook Gameroom Browser.exe"^
     /IM zapp.exe^
     /IM steamwebhelper.exe^
     /IM Steam.exe^
     /IM lync.exe
    EXIT /B
    
    
    :newwindow
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe^
     "https://www.outlook.com/"^
     "http://everybodyedits.com/"^
     "http://www.kongregate.com/accounts/username/recently-played"
    EXIT /B
    
    
    :newtab
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe^
     -new-tab "https://www.outlook.com/"^
     -new-tab "http://everybodyedits.com/"^
     -new-tab "http://www.kongregate.com/accounts/username/recently-played"
    EXIT /B
    

    你能把goto和if放在一行吗?

    但标签必须位于行的开头,并且可以在前面 按用于缩进的空格。

    建议您使用 call 而不是 goto 在许多情况下 提供良好的程序流 呼叫 转到标签,然后返回 回到呼叫方的线路。使用 转到 转到标签并 那么您可能需要使用 转到 又去了别的地方 你到处走,最终可能会迷路。所以使用 呼叫 允许将所有主代码分组在一起,并将标签作为 需要。

    taskkill 可以在一个进程中处理多个图像,因此 效率更高。

    使用 ^ 继续命令到下一行,以对齐 参数垂直。可以提高可读性和维护性。

    使用 EXIT /B 可以退出标签或脚本。 使用 EXIT 关闭通常不需要的解释器 对于脚本。

    可以使用 CHOICE 或任何其他新想法 你可能有。


    缩进标签示例。

    @echo off
    if not "%~1" == "" call :level0 %*
    exit /b
    
    :level0
    echo test indented label
        :level1
        echo "%~1"
        shift
        if not "%~1" == "" goto :level1
    echo done
    exit /b
    

    使用参数调用示例脚本,即。 main.cmd 1 2 3 .口译员会发现 :level1 即使它是缩进的。

        2
  •  0
  •   Clint Hartley    7 年前

    还没有弄清楚如何把一行和更新 michael_heath 有点我的代码:

    @echo on
    setlocal
    
    CHOICE /C ectfx /m "e do everything, c clip only, t taskkill only, f skip this, x close this"
    If errorlevel 5 EXIT /B
    If errorlevel 4 goto :choiceff
    If errorlevel 3 call :taskkillonly
    If errorlevel 2 call :cliponly
    If errorlevel 1 call :everything
    
    :choiceff
    CHOICE /C wtx /m "w open new window, t open new tab, x close this"
    If errorlevel 3 EXIT /B
    If errorlevel 2 call :newtab
    If errorlevel 1 call :newwindow
    EXIT /B
    
    
    :cliponly
    echo|set /p=username@email.com|clip
    EXIT /B
    
    
    :taskkillonly
    TASKKILL /F /T^
     /IM Greenshot.exe^
     /IM iprntctl.exe^
     /IM iprntlgn.exe^
     /IM smax4pnp.exe^
     /IM ZenNotifyIcon.exe^
     /IM ZenUserDaemon.exe^
     /IM RAVBg64.exe^
     /IM nwtray.exe^
     /IM RtkNGUI64.exe^
     /IM BingSvc.exe^
     /IM Spotify.exe^
     /IM SpotifyWebHelper.exe^
     /IM FacebookGameroom.exe^
     /IM "Facebook Gameroom Browser.exe"^
     /IM zapp.exe^
     /IM steamwebhelper.exe^
     /IM Steam.exe^
     /IM lync.exe
    EXIT /B
    
    
    :everything
    echo|set /p=username@email.com|clip
    
    TASKKILL /F /T^
     /IM Greenshot.exe^
     /IM iprntctl.exe^
     /IM iprntlgn.exe^
     /IM smax4pnp.exe^
     /IM ZenNotifyIcon.exe^
     /IM ZenUserDaemon.exe^
     /IM RAVBg64.exe^
     /IM nwtray.exe^
     /IM RtkNGUI64.exe^
     /IM BingSvc.exe^
     /IM Spotify.exe^
     /IM SpotifyWebHelper.exe^
     /IM FacebookGameroom.exe^
     /IM "Facebook Gameroom Browser.exe"^
     /IM zapp.exe^
     /IM steamwebhelper.exe^
     /IM Steam.exe^
     /IM lync.exe
    EXIT /B
    
    
    :newwindow
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe^
     "https://www.outlook.com/"^
     "http://everybodyedits.com/"^
     "http://www.kongregate.com/accounts/username/recently-played"
    EXIT /B
    
    
    :newtab
    CD /D "C:\Program Files (x86)\Mozilla Firefox"
    firefox.exe^
     -new-tab "https://www.outlook.com/"^
     -new-tab "http://everybodyedits.com/"^
     -new-tab "http://www.kongregate.com/accounts/username/recently-played"
    EXIT /B
    

    删除VAR cliportaskkill startffox 从代码和一个小bug中,只有在完成代码后才按t键终止taskkill,它会进入下一个错误级别并调用 :cliponly 代码,然后再转到下一个choice命令(在这里,我将 & goto :label 以停止此错误,但在放入一行时可能会破坏此BAT文件。)

    P、 S。 在不久的将来,我想我可以把 cliportaskkill StartFox公司 VAR返回,以便我可以将其用于并行代码或两个实例,而无需等待其他代码完成(也无需在另一个BAT文件中完成)


    I'ev将echo设置为on,此处显示代码的输出。

    >setlocal
    
    >CHOICE /C ectfx /m "e do everything, c clip only, t taskkill only, f skip this, x close this"
    e do everything, c clip only, t taskkill only, f skip this, x close this [E,C,T,F,X]?T
    
    >If errorlevel 5 EXIT /B
    
    >If errorlevel 4 goto :choiceff
    
    >If errorlevel 3 call :taskkillonly
    
    >TASKKILL /F /T /IM Greenshot.exe /IM iprntctl.exe /IM iprntlgn.exe /IM smax4pnp.exe /IM ZenNotifyIcon.ex
    e /IM ZenUserDaemon.exe /IM RAVBg64.exe /IM nwtray.exe /IM RtkNGUI64.exe /IM BingSvc.exe /IM Spotify.exe /IM SpotifyWebHelper.exe /IM F
    acebookGameroom.exe /IM "Facebook Gameroom Browser.exe" /IM zapp.exe /IM steamwebhelper.exe /IM Steam.exe /IM lync.exe
    ERROR: The process "Greenshot.exe" not found.
    ERROR: The process "iprntctl.exe" not found.
    ERROR: The process "iprntlgn.exe" not found.
    ERROR: The process "smax4pnp.exe" not found.
    ERROR: The process "ZenNotifyIcon.exe" not found.
    ERROR: The process "ZenUserDaemon.exe" not found.
    ERROR: The process "RAVBg64.exe" not found.
    ERROR: The process "nwtray.exe" not found.
    ERROR: The process "RtkNGUI64.exe" not found.
    ERROR: The process "BingSvc.exe" not found.
    ERROR: The process "Spotify.exe" not found.
    ERROR: The process "SpotifyWebHelper.exe" not found.
    ERROR: The process "FacebookGameroom.exe" not found.
    ERROR: The process "Facebook Gameroom Browser.exe" not found.
    ERROR: The process "zapp.exe" not found.
    ERROR: The process "steamwebhelper.exe" not found.
    ERROR: The process "Steam.exe" not found.
    ERROR: The process "lync.exe" not found.
    
    >EXIT /B
    
    >If errorlevel 2 call :cliponly
    
    >echo | set /p=username@email.com  | clip
    
    >EXIT /B
    
    >If errorlevel 1 call :everything
    
    >CHOICE /C wtx /m "w open new window, t open new tab, x close this"
    w open new window, t open new tab, x close this [W,T,X]?
    

    正如您在taskkill块之后看到的,它转到下一个块,即转到下一个块之前的片段代码 CHOICE commmand(由于某些原因,它不会转到下一个代码块。)

    我已经按了T键

    >If errorlevel 3 EXIT /B
    
    >If errorlevel 2 call :newtab
    
    >CD /D "C:\Program Files (x86)\Mozilla Firefox"
    
    >firefox.exe -new-tab "https://www.outlook.com/" -new-tab "http://everybodyedits.com/" -new-tab "
    http://www.kongregate.com/accounts/username/recently-played"
    
    >ECHO newtab!
    newtab!
    
    >pause
    Press any key to continue . . .
    
    >EXIT /B
    
    >If errorlevel 1 call :newwindow
    
    >CD /D "C:\Program Files (x86)\Mozilla Firefox"
    
    >firefox.exe "https://www.outlook.com/" "http://everybodyedits.com/" "http://www.kongregate.com/a
    ccounts/username/recently-played"
    
    >ECHO newwindow!
    newwindow!
    
    >pause
    Press any key to continue . . .
    

    我添加了一个 echo pause 测试它是否对此执行相同操作 选择 命令,并在完成上一个代码后启动下一个代码块。我已经在一个测试文件中对此进行了测试,其中使用了一些选项,如errorlevel、calling和goto命令以及测试文件中发生的相同错误。