Home / Microsoft / Microsoft DOS / Assigning value to a variable
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: Assigning value to a variable  (Read 1265 times)
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« on: November 20, 2005, 07:46:32 PM »

Im new to ms-dos and am having trouble assigning a value to a variable. i have read it on many sites but am still struggling. i want to use the if statement to perform a task depending on the ip address.
i.e.
if %ip%==192.168.46.1 dosomething.

how do i make the variable equal to the ip address of a particular machine? im using windows xp.
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #1 on: November 21, 2005, 05:34:03 AM »

You need to find a command that will produce the ip address as part of it's output, and parse that output. Something like this should work:

Code: [Select]

@echo off
for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n
if %ip%==192.168.46.1 dosomething


Hope this helps. 8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #2 on: November 21, 2005, 03:41:04 PM »

I tried the code and this was the response, im trying to assign a default printer and the default printer code works on its own but i want different machines to have different default printers depending on their ip address.

@echo off
for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n
%%i was unexpected at this time.
if %ip%==192.168.46.1 RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "PRINTER"
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #3 on: November 21, 2005, 04:13:14 PM »

I reproduced your error by typing in each line of code at the command prompt. The code was written for inclusion in a batch file. If you want to use the command line (can't imagine why), eliminate one of the % symbols for i, k, l, m and n variables:

Code: [Select]

for /f "tokens=1-6 delims=:. " %i in ('ipconfig ^| find /i "ip address"') do set ip=%k.%l.%m.%n
if %ip%==192.168.46.1 RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "PRINTER"


Good luck. 8)
« Last Edit: November 21, 2005, 04:14:40 PM by Sidewinder » IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #4 on: November 21, 2005, 07:28:39 PM »

IT WORKS!!! You would not believe how long ive been workin on that.... thanks heaps. My next question is how do i assign it to a range of ip addresees.
i.e. 192.168.3.1 - 192.168.3.5 "A4 Black and White" and can i use hotkeys such as *.1 - *.5?
     
IP logged

Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #5 on: November 21, 2005, 09:06:05 PM »

i figured it out using less than or equal to. thanks heaps for your help saved a lot of heartache!!!
IP logged

Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #6 on: November 23, 2005, 02:48:42 PM »

I want to add a little more to the code using the else command so that if the ip is in the range specified it will assign one printer other wise it will assign another. this is the code so far......

for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n
if %ip% leq 192.168.32.61 RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Black & White" else RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Colour"

it doesnt throw up any errors but it doesnt assign the printer a4 colour the ip address is outside the range.
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #7 on: November 23, 2005, 04:43:11 PM »

Batch files do not have many syntax rules, but try to use the ones it does have:

Code: [Select]

for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n  
if %ip% leq 192.168.32.61 (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Black & White") else (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Colour")  


Hope this helps. 8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #8 on: November 23, 2005, 06:36:43 PM »

It still doesnt work. there is no error shown it just doesnt assign the colour a4 to computers outside the ip range that i specified.
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #9 on: November 23, 2005, 07:31:45 PM »

The code as written is logically correct. I was able to run thru both the IF and the ELSE paths of the logic (both color and B&W results). What values does the %ip% variable resolve to?

In one post you mentioned a range of IP addresses (192.168.3.1 - 192.168.3.5). Keep in mind that this is a string compare, not a numeric one.

Let us know.  8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #10 on: November 23, 2005, 09:30:41 PM »

if i run the first part of the code, it works fine and assigns the black and white to the correct addresses.

@echo off
for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n  
if %ip% leq 192.168.3.21 (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Black & White")

however, when i add the else statment to it, it wont assign the colour printer to computers outside of the ip range specified.

for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n  
if %ip% leq 192.168.3.21 (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Black & White")
else (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "A4 Colour")
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #11 on: November 24, 2005, 05:04:54 AM »

You really didn't answer my question as to what %ip% resolves to. You also keep changing the value of the literal (was 192.168.32.61, now it's 192.168.3.21); it's very difficult to see what's happening if the target keeps moving and the variable remains illusive.

Next up, the IF and the ELSE must be on the same line. Not necessarily physically but logically. Type IF /? at the command prompt for full details.

If you hardcode %ip% to various values, you should be able produce both the IF and ELSE conditions (although not necessarily at the same time).

Good luck.  8)

Note: Windows Script is a better solution but since I'm batting zero for 1200, I'll not mention this. :-X
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #12 on: November 24, 2005, 03:16:16 PM »

Sorry the ip address keeps changing as i just throw in anything for security reasons  ;) um a friend actually helped me with the code and as im still very much a beginner i am unsure as to how to answer your question "what does %ip% resolve to?" im kind of muddling my way through and learning in the process. What do you mean by hardcoding %ip% to various values? Sorry to be a pain, im just having a little trouble understanding some of the terms.....  :-[
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,341

Experience: Familiar
OS: Windows 7

« Reply #13 on: November 24, 2005, 03:40:36 PM »

In order to test the code, just force each condition and see the results:

Code: [Select]

for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n  
if 192.168.3.1 leq 192.168.32.61 (echo "A4 Black & White") else (echo "A4 Colour")  

The above code will echo A4 Black & White

Code: [Select]

for /f "tokens=1-6 delims=:. " %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%k.%%l.%%m.%%n  
if 192.168.32.62 leq 192.168.32.61 (echo "A4 Black & White") else (echo "A4 Colour")  

The above code will echo A4 Colour

Now that you know the code works (both conditions), it becomes a matter of what the SET statement sets the %IP% variable to.

Hope this helps. 8)

PS. 192.168 along with other reserved IP ranges are internal network addresses. There has been no security breach.
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Blink
Topic Starter
Intermediate



Posts: 135



It just sounds to me like you need to unplug, man.

« Reply #14 on: November 24, 2005, 03:47:09 PM »

Ok now i understand what you mean, logically the statement is correct otherwise it wouldn't echo back the a4 black and white or a4 colour, so what is the easiest way for me to find out what the SET statement sets the %ip% variable to?
IP logged

Pages: [1] 2  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Assigning value to a variable « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.096 seconds with 20 queries.