如何将字符串和用户输入回显到文件

时间:2019-04-09 00:59:14

标签: batch-file

我试图将变量回显到autoexec.cfg文件中,但是在同时将字符串和变量回显到文件时遇到了一些麻烦。

我已经尝试在echo语句周围加上引号,例如

echo "sv_maxplayers %answer%">%location%

,并且在正确回显字符串的同时,还包括引号。 ("sv_maxplayers 4"

我的脚本当前为

@ECHO OFF
    set location=".\Risk of Rain 2_Data\Config\autoexec.cfg"
    set /p answer="Please enter a number 1-16:"
    if %answer% GTR 0 (
        if %answer% LSS 17 (
            goto set
        )
    )
    echo Invalid argument.
    goto exit

:set    
    echo sv_maxplayers %answer%>%location%
    echo Done!
    goto exit

:exit
    pause
    exit

运行返回

Please enter a number 1-16:4
sv_maxplayers
Done!
Press any key to continue . . .
在控制台中

并清除autoexec.cfg。

它应该输出为

sv_maxplayers 4

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

找到了解决方案!我错过了一些空格。

echo sv_maxplayers %answer%>%location% 需要是 echo sv_maxplayers %answer% > %location%