批处理语法不正确?

时间:2014-11-15 00:36:13

标签: batch-file syntax syntax-error

@echo off
title LocalTransmit Messaging Service Version 1.0
color f0
:startup
start run.cmd
set /P locationVal="Enter where you are (home or school):"
if "%locationVal%"=="home" (
set place=C:\Users\Andrew\Data\Chat\chatroom.chatfile
echo %computername% has joined the chatroom! >> %place%
goto message
)
if "%locationVal%"=="school" (
set place=\\HS-LAB-12\Users\student\Data\Chat\chatroom.chatfile
echo %computername% has joined the chatroom! >> %place%
goto message
)
goto error

所以上面的代码是短消息程序的开始代码。当突然上面的代码导致命令提示符窗口关闭并导致&#34时,我试图使其更加用户友好;"命令的语法不正确"在窗口关闭之前短暂出现在提示下方。我无法弄清楚我的语法有什么问题。非常感谢任何帮助。

P.S。 .chatfile是由该程序的先前版本生成的,其作用类似于普通文本文件。

2 个答案:

答案 0 :(得分:1)

标准delayedexpansion问题 - 搜索SO - 它被覆盖了数百次。

在块语句(a parenthesised series of statements)中,解析整个块并执行然后。块中的任何%var%将在解析块时被该变量的值替换 - 在块执行之前 - 同样的事情适用于FOR ... DO (block)。< / p>

在您的情况下,place中的block正在被更改,因此您需要使用其中一种机制来解决问题。

在例程开始时,place未定义,因此您的代码已解析为

echo %computername% has joined the chatroom! >>

因此语法错误报告。

尝试从块语句中删除该行,并将其放在:message标签之后:

:message
echo %computername% has joined the chatroom! >> %place%

您的代码将变得更便宜。

答案 1 :(得分:0)

在上面的代码顶部附近添加。你得到了什么?那里有特殊的角色?

echo(%ComputerName%
pause
相关问题