DosTips.com

A Forum all about DOS Batch
It is currently 28 Aug 2016 04:18

All times are UTC-06:00




Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next
Author Message
PostPosted: 10 Jul 2012 14:03 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
I am writing a batch file to look for just Dell computers. The problem is that the value this will return on Dell computers can be Dell Inc., Dell or whatever I need to make it look for just the word Dell. Since I believe that Dell will always be the first four characters I am parsing the 1st variable four the first 4 characters. I have almost completed the part that identifies the manufacturer and then uses a goto command to send it to the correct place in the batch. It errors out at the IF %_prefix%=Dell on the 7th line with Dell not expected yet. I would even be willing to entertain a different way of doing it but i am very curios why this is not working. Everything above that works fine. Can someone help solve this puzzle?
Thanks in advance for your help.

@ECHO OFF
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Vendor /VALUE ^| FIND /I "Vendor="') DO SET machine=%%A
ECHO Computer model: "%machine%"

set _prefix==%machine:~0,4%
echo %_prefix%
IF %_prefix%=Dell (
goto setup
) ELSE (
goto end
)


:setup
echo success

:end
echo not yet
pause


Top
   
PostPosted: 10 Jul 2012 14:50 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3169
You need to use two equals symbols with your IF comparison. You should also look a using the /I switch.

Edit: Just noticed your set statement is using two equal symbols when you only need one.


Top
   
PostPosted: 10 Jul 2012 14:58 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Thanks i changed that but it had no effect


Top
   
PostPosted: 10 Jul 2012 15:12 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Here are some small changes i have made to the code the results in the DOS screen are shown below that. It seems that now there is something wrong with the ELSE statement. It shouldn't be showing both the last 2 echo's just one or the other. Or is this the way it should come out?
rem @ECHO OFF
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Vendor /VALUE ^| FIND /I "Vendor="') DO SET machine=%%A
ECHO Computer model: "%machine%"

set prefix==%machine:~0,4%
IF %prefix%==Dell (
GOTO setup
) ELSE (
GOTO end
)


:setup
echo success

:end
echo not yet
pause

C:\Users\JimT\Desktop>rem @ECHO OFF

C:\Users\JimT\Desktop>FOR /F "tokens=2 delims==" %A IN ('WMIC csproduct GET Vend
or /VALUE | FIND /I "Vendor="') DO SET machine=%A

:\Users\JimT\Desktop>SET machine=Dell Inc.

C:\Users\JimT\Desktop>ECHO Computer model: "Dell Inc."
Computer model: "Dell Inc."

C:\Users\JimT\Desktop>set prefix==Dell

C:\Users\JimT\Desktop>IF Dell == Dell (GOTO setup ) ELSE (GOTO end )

C:\Users\JimT\Desktop>echo success
success

C:\Users\JimT\Desktop>echo not yet
not yet


Top
   
PostPosted: 10 Jul 2012 16:08 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3169
You didn't change the set statement.


Top
   
PostPosted: 10 Jul 2012 16:14 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Oops it slipped back in here are the new results
rem @ECHO OFF
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Vendor /VALUE ^| FIND /I "Vendor="') DO SET machine=%%A
ECHO Computer model: "%machine%"

set prefix=%machine:~0,4%
ECHO %prefix%
IF %prefix%==Dell (
GOTO setup
) ELSE (
GOTO end
)


:setup
echo success

:end
echo not yet
pause > nul


C:\Users\JimT\Documents\scripts>rem @ECHO OFF

C:\Users\JimT\Documents\scripts>FOR /F "tokens=2 delims==" %A IN ('WMIC csproduc
t GET Vendor /VALUE | FIND /I "Vendor="') DO SET machine=%A

:\Users\JimT\Documents\scripts>SET machine=Dell Inc.

C:\Users\JimT\Documents\scripts>ECHO Computer model: "Dell Inc."
Computer model: "Dell Inc."

C:\Users\JimT\Documents\scripts>set prefix=Dell

C:\Users\JimT\Documents\scripts>ECHO Dell
Dell

C:\Users\JimT\Documents\scripts>IF Dell == Dell (GOTO setup ) ELSE (GOTO end )


C:\Users\JimT\Documents\scripts>echo success
success

C:\Users\JimT\Documents\scripts>echo not yet
not yet


Top
   
PostPosted: 10 Jul 2012 16:16 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Without the pause > nul
C:\Users\JimT\Documents\scripts>FOR /F "tokens=2 delims==" %A IN ('WMIC csproduc
t GET Vendor /VALUE | FIND /I "Vendor="') DO SET machine=%A

:\Users\JimT\Documents\scripts>SET machine=Dell Inc.

C:\Users\JimT\Documents\scripts>ECHO Computer model: "Dell Inc."
Computer model: "Dell Inc."

C:\Users\JimT\Documents\scripts>set prefix=Dell

C:\Users\JimT\Documents\scripts>ECHO Dell
Dell

C:\Users\JimT\Documents\scripts>IF Dell == Dell (GOTO setup ) ELSE (GOTO end )


C:\Users\JimT\Documents\scripts>echo success
success

C:\Users\JimT\Documents\scripts>echo not yet
not yet

C:\Users\JimT\Documents\scripts>pause
Press any key to continue . . .


Top
   
PostPosted: 10 Jul 2012 21:35 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5907
@ECHO OFF
set "machine="
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Vendor /VALUE ^| FIND /I "Vendor=" ^| find /i "Dell" ') DO set machine=%%a

If defined machine (
echo it's a Dell
) else (
echo it ain't a Dell
)


Top
   
PostPosted: 10 Jul 2012 23:26 
Offline
Expert

Joined: 13 Jan 2012 21:24
Posts: 469
gymiv wrote:
It seems that now there is something wrong with the ELSE statement. It shouldn't be showing both the last 2 echo's just one or the other. Or is this the way it should come out?
Nothing wrong with ELSE and, yes, that's what's supposed to come out.

You probably want a "goto :eof" or similar after "echo success", otherwise it's simply falling through and continuing to run the ":end" portion.

Liviu


Top
   
PostPosted: 11 Jul 2012 02:46 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
why you go through all this troubles why don't you just use "WMIC csproduct get vendor"
and skip the first line in a For loop and then take the first token

Quote:
FOR /F "skip=1 tokens=1" %%A in ('WMIC csproduct get vendor') Do Set Machine=%%A


And If you add
Code:
Echo %Machine%

at the end of the code and a pause command you get the vendor name
I tested on my PC and my vendor is gigabyte and it work fine

Here is a Test batch:
Code:
@echo off
cls
For /F "skip=1 tokens=1" %%A IN ('WMIC csproduct get vendor') DO echo %%A
pause

and one ore thing in all functions you make ":end" and ":setup" like liviu said must end with goto EOF to prevent the batch from continuing to the other functions
and test your code from a batch not from the command line.


Top
   
PostPosted: 11 Jul 2012 07:47 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
In answer to your question, you have to remember when you do a WMIC csproduct GET Vendor you could get Dell Inc, or just Dell or however they want to display it. However it is a pretty safe bet that the first 4 letters are going to be Dell. That is why I need to parse the variable from the WMIC csproduct GET Vendor command. Otherwise if i get Dell Inc. for the IF command then it goes to the wrong section. I did get it working fine the piece I had forgot was the :eof. Thanks all. Here is my final code.

@ECHO OFF
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Vendor /VALUE ^| FIND /I "Vendor="') DO SET machine=%%A
ECHO Computer model: "%machine%"

set prefix=%machine:~0,4%
ECHO %prefix%
IF %prefix%==Dell (
GOTO setup
) ELSE (
GOTO end
)


:setup
echo success
pause > nul
GOTO :eof


:end
echo not yet
pause > nul
GOTO :eof


Top
   
PostPosted: 11 Jul 2012 07:56 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3169
gymiv you are missing the point of the previous script. FOR LOOPS delimit the output using spaces by default. If you use ABC's code it should always output as Dell because it will delimit by a space and remove the "Inc."

Test his FOR LOOP.


Top
   
PostPosted: 11 Jul 2012 09:35 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Tested this and could not get his command to work. Here is the code and result. Do i need to put the find statement back in?

REM @ECHO OFF
FOR /F "tokens=1" %%A in ('WMIC csproduct get vendor') Do Set Machine=%%A
ECHO Computer model: "%machine%"

IF %machine%==Dell (
GOTO :setup
) ELSE (
GOTO :end
)


:setup
echo success
pause > nul
GOTO :eof


:end
echo not yet
pause > nul
GOTO :eof

C:\Users\JimT\Desktop>REM @ECHO OFF

C:\Users\JimT\Desktop>FOR /F "tokens=1" %A in ('WMIC csproduct get vendor') Do S
et Machine=%A

C:\Users\JimT\Desktop>Set Machine=Vendor

C:\Users\JimT\Desktop>Set Machine=Dell

:\Users\JimT\Desktop>Set Machine=

C:\Users\JimT\Desktop>ECHO Computer model: ""
Computer model: ""
( was unexpected at this time.
==Dell (\JimT\Desktop>IF
C:\Users\JimT\Desktop>


Top
   
PostPosted: 11 Jul 2012 09:39 
Offline

Joined: 10 Jul 2012 13:47
Posts: 38
Sorry left out the skip when i was testing it. Here is the code with the skip in it. Still no go

REM @ECHO OFF
FOR /F "skip=1 tokens=1" %%A in ('WMIC csproduct get vendor') Do Set Machine=%%A
ECHO Computer model: "%machine%"

IF %machine%==Dell (
GOTO :setup
) ELSE (
GOTO :end
)


:setup
echo success
pause > nul
GOTO :eof


:end
echo not yet
pause > nul
GOTO :eof

C:\Users\JimT\Desktop>REM @ECHO OFF

C:\Users\JimT\Desktop>FOR /F "skip=1 tokens=1" %A in ('WMIC csproduct get vendor
') Do Set Machine=%A

C:\Users\JimT\Desktop>Set Machine=Dell

:\Users\JimT\Desktop>Set Machine=

C:\Users\JimT\Desktop>ECHO Computer model: ""
Computer model: ""
( was unexpected at this time.
==Dell (\JimT\Desktop>IF
C:\Users\JimT\Desktop>


Top
   
PostPosted: 11 Jul 2012 09:51 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3169
Don't know what to tell you. It works fine on my computer.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited