Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Batch- Multiple word variables?

Original Message
Name: Carts
Date: February 18, 2008 at 12:46:14 Pacific
Subject: Batch- Multiple word variables?
OS: Windows XP
CPU/Ram: AMD Athlon XP 3200+ 2.21
Model/Manufacturer: 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


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: February 18, 2008 at 23:27:48 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
In your IF statements, don't use single quotes. Instead, use double quotes or parentheses.

Report Offensive Follow Up For Removal

Response Number 2
Name: klint
Date: February 19, 2008 at 02:01:09 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: February 19, 2008 at 18:16:04 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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.

Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: February 20, 2008 at 00:44:10 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 5
Name: klint
Date: February 20, 2008 at 03:21:34 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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!


Report Offensive Follow Up For Removal


Response Number 6
Name: Carts
Date: February 20, 2008 at 05:25:41 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
Alright. I'll try it.

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

Babaloomba


Report Offensive Follow Up For Removal

Response Number 7
Name: Carts
Date: February 20, 2008 at 05:30:43 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
Or refresh switches in Internet Explorer?

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

Babaloomba


Report Offensive Follow Up For Removal

Response Number 8
Name: klint
Date: February 20, 2008 at 06:28:39 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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.

Report Offensive Follow Up For Removal

Response Number 9
Name: Carts
Date: February 20, 2008 at 13:49:26 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 10
Name: klint
Date: February 20, 2008 at 16:06:04 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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!


Report Offensive Follow Up For Removal

Response Number 11
Name: Razor2.3
Date: February 20, 2008 at 17:48:35 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 12
Name: Mechanix2Go
Date: February 20, 2008 at 18:40:20 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
Hi klint,

Good points to ponder.


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

M2



Report Offensive Follow Up For Removal

Response Number 13
Name: Carts
Date: February 20, 2008 at 20:47:20 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 14
Name: klint
Date: February 21, 2008 at 02:15:05 Pacific
Subject: Batch- Multiple word variables?
Reply: (edit)
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?


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch- Multiple word variables?

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC