需要输入 .bat 文件附加文本行(半工作)

need input for a .bat file appending lines of text(semi-working)

前言:创建批处理文件已经有一段时间了,这是我第一次使用这个网站,所以如果 我忽略或忘记做某事,只需指出即可,我会修复它。

今天我决定重新研究一些 dos 游戏,我想制作一个批处理文件以允许我追加三个 文本行,本质上是您通常手动编写的 mount->c:->.exe 行,到 [autoexec] 部分 dosbox.conf.

经过一天的修修补补,我终于得到了一些游戏的批处理文件,warcraft2/DungeonMaster/LandsofLore, 我现在遇到的唯一问题是代码对上面提到的三个都可以正常工作,但其他的就不行 甚至附加到 dosbox.conf.

(在'debugging'之后,使用了魔兽争霸1) 我在 'set/p input' 之后和 war1 的 if 语句中放置了暂停命令,也在 war1 标签中,开始和结束。触发的唯一暂停是 'set/p input' 处的暂停,因此代码要么忽略了 If 语句,要么我不知道自己在做什么。

任何关于我做错了什么的指示将不胜感激。 TL:DR 代码适用于 warcraft2(输入 w)、Lands of Lore(输入 l)、dungeon master(输入 d)其他输入似乎要么在输入后立即退出,要么在到达 IF 之前取消 statement/s

 @echo off

REM ask is currently not used
:ask
REM asks for input
echo Which game would you like to play?
echo ___________________________________________________________________________________________
echo [w]arcraft 2 - w[a]rcraft1 - [c]ivilization - d[u]ne 2 - [d]ungeon master
echo [l]ands of lore - [o]regontrail - [s]im ant - Wol[f]3D 
echo -------------------------------------------------------------------------------------------

REM input variable
setlocal
SET /P INPUT=[]

REM check user input then go to selected game
IF /I "%INPUT%"=="w" (
    goto war2
)
IF /I "%INPUT%"=="d" (
    goto dungeon
)
IF /I "%INPUT%"=="l" (
    goto land
)
IF /I "%INPUT%"=="a"(
    goto war1
)
IF /I "%INPUT%"=="c"(
    goto civ
)
IF /I "%INPUT%"=="u"(
    goto dune2
)
IF /I "%INPUT%"=="o"(
    goto trail
)
IF /I "%INPUT%"=="s"(
    goto ant
)
IF /I "%INPUT%"=="f"(
    goto wolf
)

REM appends text to the end of dosbox.conf,after the [autoexec]
REM then starts dosbox and ends the file.
:war1
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\war1\  -label warcraft1>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO WAR>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:civ
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\civ\  -label Civ>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO CIV>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:dune2
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\dun2\  -label Dune 2>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO DUNE2>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:trail
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\oregontrail\  -label Oregon Trail>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO OREGON>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:ant
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\simant\  -label Sim Ant>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO SIMANT>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:wolf
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\WOLF3D\  -label Oregon Trail>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO WOLFD3D>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:war2
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\war2\war2\  -label warcraft2>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO war2>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:dungeon
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf" 
    ECHO mount c f:\DOSGames\DungeonMaster\ -t cdrom -label DungeonMaster>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO dm>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE
goto:EOF
:land   
    ECHO mount -u c>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO mount c f:\DOSGames\LoL\ -label lands>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO c:>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
    ECHO lands>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
 start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE

你 运行 来自命令提示符的脚本而不是 double-clicking 它,你会看到你收到 "The syntax of the command is incorrect" 错误。

这是因为您在每个 if 语句中的 ( 之前都缺少 space,魔兽争霸 2、地下城主和传说之地除外。

IF /I "%INPUT%"=="w" (
    goto war2
)
IF /I "%INPUT%"=="d" (
    goto dungeon
)
IF /I "%INPUT%"=="l" (
    goto land
)
IF /I "%INPUT%"=="a" (
    goto war1
)
IF /I "%INPUT%"=="c" (
    goto civ
)
IF /I "%INPUT%"=="u" (
    goto dune2
)
IF /I "%INPUT%"=="o" (
    goto trail
)
IF /I "%INPUT%"=="s" (
    goto ant
)
IF /I "%INPUT%"=="f" (
    goto wolf
)

您也可以通过将 gotoif 放在同一行而不带括号来避免这种情况。

IF /I "%INPUT%"=="w" goto war2
IF /I "%INPUT%"=="d" goto dungeon
IF /I "%INPUT%"=="l" goto land
IF /I "%INPUT%"=="a" goto war1
IF /I "%INPUT%"=="c" goto civ
IF /I "%INPUT%"=="u" goto dune2
IF /I "%INPUT%"=="o" goto trail
IF /I "%INPUT%"=="s" goto ant
IF /I "%INPUT%"=="f" goto wolf

您在输入值尾随双引号和不起作用的行中的左括号之间缺少 space。

不过,我建议进行此更改,以便更轻松地管理代码。

对重复的条目使用变量并创建一个循环以返回“询问”以及“退出”选项。

@echo off
setlocal

:: ask is currently not used
:ask
:: asks for input
cls
echo Which game would you like to play?
echo ___________________________________________________________________________________________
echo [w]arcraft 2 - w[a]rcraft1 - [c]ivilization - d[u]ne 2 - [d]ungeon master
echo [l]ands of lore - [o]regontrail - [s]im ant - Wol[f]3D  - [Q]uit
echo -------------------------------------------------------------------------------------------

:: input variable
SET /P INPUT=[]

IF /I "%INPUT%"=="q" then Exit

set DosBoxConf="F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"
set MountDrive=mount -u c
set MyDrive=C:

:: check user input then go to selected game
IF /I "%INPUT%"=="w" ( 
set MountCmd=mount c f:\DOSGames\war2\war2\  -label warcraft2
set RunCmd=WAR2
goto RunDosBox
) 
IF /I "%INPUT%"=="d" ( 
set MountCmd=mount c f:\DOSGames\DungeonMaster\ -t cdrom -label DungeonMaster
set RunCmd=DM
goto RunDosBox
) 
IF /I "%INPUT%"=="l" ( )
set MountCmd=mount c f:\DOSGames\LoL\ -label lands
set RunCmd=lands
goto RunDosBox
) 
IF /I "%INPUT%"=="a" ( 
set MountCmd=mount c f:\DOSGames\war1\  -label warcraft1
set RunCmd=WAR
goto RunDosBox
) 
IF /I "%INPUT%"=="c" ( 
set MountCmd=mount c f:\DOSGames\civ\  -label Civ
set RunCmd=CIV
goto RunDosBox
) 
IF /I "%INPUT%"=="u" ( 
set MountCmd=mount c f:\DOSGames\dun2\  -label Dune 2
set RunCmd=DUNE2
goto RunDosBox
) 
IF /I "%INPUT%"=="o" ( 
set MountCmd=mount c f:\DOSGames\oregontrail\  -label Oregon Trail
set RunCmd=OREGON
goto RunDosBox
) 
IF /I "%INPUT%"=="s" ( 
set MountCmd=mount c f:\DOSGames\simant\  -label Sim Ant
set RunCmd=SIMANT
goto RunDosBox
) 
IF /I "%INPUT%"=="f" ( 
set MountCmd=mount c f:\DOSGames\WOLF3D\  -label Wolf 3D
set RunCmd=WOLFD3D
goto RunDosBox
) 
:: back to ask if input didn't match a choice
goto ask

:RunDosBox
:: build the config and run DosBox
ECHO %MountDrive%>>%DosBoxConf%
ECHO %MountCmd%>>%DosBoxConf%
ECHO %MyDrive%>>%DosBoxConf%
ECHO %RunCmd%>>%DosBoxConf%
start F:\Programs\DOSBoxPortable\DOSBoxPortable.EXE

另请注意,您代码中的这一行有一个不正确的 Oregon Trail 标签,但该部分适用于 Wolf3D。

ECHO mount c f:\DOSGames\WOLF3D\  -label Oregon Trail>>"F:\Programs\DOSBoxPortable\Data\settings\dosbox.conf"