Can you Echo an if command? Is there a workaround if not? Batch

时间:2016-03-04 17:47:05

标签: batch-file if-statement echo

So I have the following batch code:

:AddMember
cls 
title Add Member
color 0A
set /p membername=Member name?
echo %membername% >>C:\Users\187242\Desktop\swag\members.txt
set /p memberpass=Member Password?
echo %memberpass% >>C:\Users\187242\Desktop\swag\memberspass.txt
echo :%membername% >>C:\Users\187242\Desktop\swag\database.bat
echo cls >>C:\Users\187242\Desktop\swag\database.bat
echo title >>C:\Users\187242\Desktop\swag\database.bat
echo color 0a >>C:\Users\187242\Desktop\swag\database.bat
echo echo Member: %membername% >>C:\Users\187242\Desktop\swag\database.bat
echo echo Password: %memberpass% >>C:\Users\187242\Desktop\swag\database.bat
echo set /p Memberoptions=What Would you like to do?     >>C:\Users\187242\Desktop\swag\database.bat
echo echo 1. Nothing >>C:\Users\187242\Desktop\swag\database.bat
echo echo 2. Change Password >>C:\Users\187242\Desktop\swag\database.bat
echo echo 3. Change Username >>C:\Users\187242\Desktop\swag\database.bat
echo echo 4. DELETE User >>C:\Users\187242\Desktop\swag\database.bat
echo if "%memberoptions%"=="1" goto :Enter
echo if "%memberoptions%"=="2" goto 

In the last two lines I'm trying to get the program to write the if command into another block of code, is this possible? If not is there a workaround i just havent thought of?

2 个答案:

答案 0 :(得分:3)

更容易构建

(
echo cls
echo title
echo color 0a 
)>>C:\Users\187242\Desktop\swag\database.bat

括号确保将echo重定向到文件。我用>创建了一个新文件。 >>将附加到任何现有文件(如果当前不存在,则创建新文件)

诀窍是将每个 %加倍,这需要echo到目标文件。这是因为批量处理%escape character的{​​{1}}。 %用于暂时打开或关闭escape character字符的特殊含义。在早期应用程序中, Esc 在一系列其他可打印字符之前被发送到打印机,以控制打印机的下划线,粗体,斜体和其他功能。

还有其他字符需要批量escaped,尤其是escape > <|。这些需要)caret)作为逃避。 ^是需要%作为转义的例外。

答案 1 :(得分:2)

It looks like you're goal is to write these two IF commands into database.bat as well. For that to work you need to escape the percent signs by doubling them:

echo if "%%memberoptions%%"=="1" goto :Enter
echo if "%%memberoptions%%"=="2" goto 

Output:

if "%memberoptions%"=="1" goto :Enter
if "%memberoptions%"=="2" goto