ajc2000
ajc2000

Reputation: 834

Syntax incorrect with batch?

@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

So the above code is the beginning code for a short messaging program. I was attempting to make it more user-friendly when suddenly the above code resulted in the command prompt window closing and results in "Syntax of the command is incorrect" appearing below the prompt briefly before the window closes. I cannot figure out what is wrong with my syntax. Any help is greatly appreciated.

P.S. the .chatfile was generated by a previous version of this program and acts like a normal text file.

Upvotes: 0

Views: 60

Answers (2)

Magoo
Magoo

Reputation: 79982

Standard delayedexpansion problem - search SO - it's been covered hundreds of times.

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

In your case, place is being altered in the block so you need to use one of the mechanisms to get around the problem.

At the start of the routine, place is undefined, so your code is resolved to

echo %computername% has joined the chatroom! >>

hence the syntax-error report.

Try removing that line from the block statements and put it after the :message label:

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

Your code will become shorter into the bargain.

Upvotes: 1

RGuggisberg
RGuggisberg

Reputation: 4750

Add near the top of the code above. What do you get? Special character(s) in there?

echo(%ComputerName%
pause

Upvotes: 0

Related Questions