Computing.Net > Forums > Programming > Batch- Multiple word variables?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch- Multiple word variables?

Reply to Message Icon

Name: Carts
Date: February 18, 2008 at 12:46:14 Pacific
OS: Windows XP
CPU/Ram: AMD Athlon XP 3200+ 2.21
Product: eMachines
Comment:

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
cls

start 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 start2

It 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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: February 18, 2008 at 23:27:48 Pacific
Reply:

In your IF statements, don't use single quotes. Instead, use double quotes or parentheses.


0

Response Number 2
Name: klint
Date: February 19, 2008 at 02:01:09 Pacific
Reply:

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


0

Response Number 3
Name: Razor2.3
Date: February 19, 2008 at 18:16:04 Pacific
Reply:

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.


0

Response Number 4
Name: Mechanix2Go
Date: February 20, 2008 at 00:44:10 Pacific
Reply:

This works with multiple words:

::==
@echo off
setLocal EnableDelayedExpansion

set /p var= what?
echo !var!



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 5
Name: klint
Date: February 20, 2008 at 03:21:34 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: Carts
Date: February 20, 2008 at 05:25:41 Pacific
Reply:

Alright. I'll try it.

Still, are there any command line switches in Firefox to get it to refresh?

Babaloomba


0

Response Number 7
Name: Carts
Date: February 20, 2008 at 05:30:43 Pacific
Reply:

Or refresh switches in Internet Explorer?

I don't think that the computer I'll be using this on has Firefox.

Babaloomba


0

Response Number 8
Name: klint
Date: February 20, 2008 at 06:28:39 Pacific
Reply:

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.


0

Response Number 9
Name: Carts
Date: February 20, 2008 at 13:49:26 Pacific
Reply:

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


0

Response Number 10
Name: klint
Date: February 20, 2008 at 16:06:04 Pacific
Reply:

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 clear

I hope this makes sense. Good luck!


0

Response Number 11
Name: Razor2.3
Date: February 20, 2008 at 17:48:35 Pacific
Reply:

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


0

Response Number 12
Name: Mechanix2Go
Date: February 20, 2008 at 18:40:20 Pacific
Reply:

Hi klint,

Good points to ponder.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 13
Name: Carts
Date: February 20, 2008 at 20:47:20 Pacific
Reply:

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


0

Response Number 14
Name: klint
Date: February 21, 2008 at 02:15:05 Pacific
Reply:

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?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch- Multiple word variables?

Batch - Get variable from text file www.computing.net/answers/programming/batch-get-variable-from-text-file/17052.html

Batch calculation on variables www.computing.net/answers/programming/batch-calculation-on-variables/16258.html

Batch to parse .txt for words www.computing.net/answers/programming/batch-to-parse-txt-for-words/16839.html