Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm making a sort of chat thing. It uses text input as a variable to add text to a common "chat" text file, and opens an internet browser viewing the file. Each chatter would have their own version of this file that embeds a name into all of their messages.
This is my code:
@echo off
clsstart Firefox.exe "C:\Documents and Settings\Owner\Desktop\Batch Stuff\Commune.txt"
:start
set /p var=Type something (write in quotes)... (press F5 in Firefox to load new text)IF '%var%' == 'cls' GOTO clear
echo (Carts)%var%>>Commune.txt:start2
cls
echo Type something... (press F5 to load new text)
set /p car=IF '%car%' == 'cls' GOTO clear
echo (Carts)%car%>>Commune.txt
cls
GOTO start2:clear
type nul > Commune.txt
GOTO start2It works as long as you only write one word, or if you write in quotes. Is there a way you can get it so you don't have to write in quotes? Not totally necessary, but it's a hassle to put quotes on any time you write more than one word. Also, is there any way to get firefox to do incremental refreshes so you don't have to refresh it every time?
Babaloomba

Razor,
I always use double quotes. I've never heard you could use parentheses. I've just tried it and couldn't get it to work. Can you give me an example of how IF works with multiple words in parentheses?
Thanks,
Klint

klint: Can you give me an example of how IF works with multiple words in parentheses?
Actually, I can't say I ever use that form. I've seen around online, but I never cared enough to investigate further.

This works with multiple words:
::==
@echo off
setLocal EnableDelayedExpansionset /p var= what?
echo !var!
=====================================
If at first you don't succeed, you're about average.M2

After experimentation, I've discovered that it's better to use delayed expansion when you have an IF statement. So, from now on, I will no longer use IF "%a%" == "..." but instead use IF "!a!" == "..." as this works in all cases, including when the variable a contains embedded quotes!

Alright. I'll try it.
Still, are there any command line switches in Firefox to get it to refresh?
Babaloomba

Or refresh switches in Internet Explorer?
I don't think that the computer I'll be using this on has Firefox.
Babaloomba

I don't think there are any command-line switches to get an existing instance, or existing window, of Internet Explorer to refresh. However, it is possible to send DDE commands to Internet Explorer to get it to do so. I know it can be done because, when you are looking at a web site, and you open Windows Explorer and go Tools -> Folder Options -> View -> Apply to All Folders, this will make all instances of Internet Explorer refresh. However, I don't know the DDE commands required to do this.

Well I tried using the double quotes, instead of letting me type only one word, it lets me type only two.
Not wanting to deal with it, I just moved the IF statement to a separate file specifically for clearing the document. Now it works perfectly.
Maybe the "delayed expansion" will work. I didn't try it, because I had never heard of it. Would someone explain it for me? (I'm relatively new to batch.)
klint, thanks for the tip about the DDE, I'll check it out.
Babaloomba

Let me explain about delayed expansion. First, let's look at the context of the problem. In your original code, you had:
IF '%var%' == 'cls' GOTO clear
The problem with that is that the single quote is not treated as anything special. It's just another character. You might as well have written:
IF a%var%a == aclsa GOTO clear
Obviously, your intention was to get the IF statement to compare two strings. Normally, the IF statement can compare two words, so you can write this:
IF %var% == cls GOTO clear
This will work only if %var% contains a single word. The % percent-sign operator causes the variable to be replaced by its value before the whole line is parsed. So if var contains the word Fred, the above line will first be expanded to this:
IF Fred == cls GOTO clear
which is perfectly good syntax. It compares two words, they are not equal so it doesn't goto clear.
Now if var contained the phrase "two words" (without the quotes), the above would become:
IF two words == cls GOTO clear
This is a syntax error because after IF the command processor expects to see a word followed by ==. To fix this, you can use double quotes, which makes the parser treat the phrase as a single word:
IF "%var%" == "cls" GOTO clear
which expands to
IF "two words" == "cls" GOTO clear
which again is perfectly valid syntax.
What if var contained the string between the parentheses here: (two" == "words) ?
Now this:
IF "%var%" == "cls" GOTO clear
expands to:
IF "two" == "words" == "cls" GOTO clear
which is a syntax error because it expects a valid command in place of the second ==.
What can we do here? The answer: use delayed expansion. To use delayed expansion, you first have to enable it at the start of your batch file with this command:
setlocal EnableDelayedExpansion
Now, you can use !var! instead of %var%. The difference is that the variable expansion is done after parsing, not before. So our solution to this problem is the following:
IF !var! == cls GOTO clear
The syntax is valid, because it is checked before !var! is expanded. var can in fact contain anything you wish. It can contain multiple words separated by spaces, or commas, or it can contain any punctuation character you like (including quotes.) Whatever it contains, it will be compared with the word cls with the results you expect.
If the right-hand side contains multiple words, you need to use quotes:
IF "!var!" == "multiple words" GOTO clear
Or you can do this:
SET test=multiple words
IF !var! == !test! GOTO clearI hope this makes sense. Good luck!

Carts: Or refresh switches in Internet Explorer?
VBScript can control it. The downside is said script must be the one to open IE.
QnD example follows:Set ie = WScript.CreateObject("InternetExplorer.Application", "IE_")
With ie
.Visible = True
.Navigate "whatever"
On Error Resume Next 'Avoids an error message when we close IE
While True
Do While .Busy
WScript.Sleep 100
Loop
WScript.Sleep 600000
.Refresh
Wend
End With

Hi klint,
Good points to ponder.
=====================================
If at first you don't succeed, you're about average.M2

Thank you klint. This will help a lot.
There's only thing I don't understand still.
Why, when I had double quotes, it only let me type two words?
Babaloomba

Babaloomba,
"Why, when I had double quotes, it only let me type two words?"
I don't know. What happened if you wrote one or three words instead?

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |