Run exe if "model number"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Run exe if "model number"

#1 Post by aristosv » 07 Nov 2013 02:04

I want a batch file to run an executable, but only if the laptop model number is 4242RR7
To get the laptop model number, I run "wmic csproduct get name". This returns:

Name
4242RR7

How can I make the batch file to run an executable only if the laptop model is 4242RR7?

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run exe if "model number"

#2 Post by foxidrive » 07 Nov 2013 03:36

You pipe the result through find or findstr, looking for the string, and then use the errorlevel to decide what to do.

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: Run exe if "model number"

#3 Post by aristosv » 07 Nov 2013 05:46

Would i be asking too much for an example? :|

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run exe if "model number"

#4 Post by foxidrive » 07 Nov 2013 06:39

As it would seem to be a class assignment, show us what you think it should look like.

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: Run exe if "model number"

#5 Post by aristosv » 07 Nov 2013 07:13

It's not a class assignment :D I wish it was..

The LAN team needs to deploy windows 7 on 350 laptops, so I am creating a USB stick with an unattended windows 7 installation which will also install all the required software for the laptops.
My problem is that not all laptops are the same model. We have different models and for each model I will have to silently install a different set of drivers.

So I am using "Double Driver" to silently restore the drivers for each newly formatted laptop (works great by the way).
My problem is that the LAN Administrator has to browse the USB stick, and double-click the batch file of the corresponding laptop model, to install the drivers for it.
Whereas if I could have the batch file detect the laptop model, I would have it run the correct batch file, to install the drivers without any input from the LAN Administrator.

I have used if statements before in batch files, but only to delete shortcuts from the desktop for windows xp and windows 7

Code: Select all

IF EXIST "%Public%\Desktop\Adobe Reader XI.lnk" (DEL "%Public%\Desktop\Adobe Reader XI.lnk")
IF EXIST "%ALLUSERSPROFILE%\Desktop\Adobe Reader XI.lnk" (DEL "%ALLUSERSPROFILE%\Desktop\Adobe Reader XI.lnk")


I would assume something similar would work for what I am trying to do, but first I would have to output the laptop model somewhere...

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Run exe if "model number"

#6 Post by Magialisk » 08 Nov 2013 03:36

I literally just threw this together to prove the concept and it's 1:30am so hopefully nobody jumps on me for doing it in the quickest dirtiest way possible, but here's the gist:

findtest.bat:

Code: Select all

@echo off
echo raw wmic output:
wmic csproduct get name

echo piping to find limits what gets returned:
wmic csproduct get name | find "EP"
wmic csproduct get name | find "4242RR7"

echo using this is a FOR can set the result to a variable
set model=
FOR /F %%i IN ('wmic csproduct get name ^| find "4242RR7"') DO set model=%%i
echo model=%model%
FOR /F %%i IN ('wmic csproduct get name ^| find "EP"') DO set model=%%i
echo model=%model%

echo.
echo Finally, a simple IF statement to check the model
IF %model%==4242RR7 (echo ---Run Executable) ELSE (echo ---Don't Run Executable)
Results:

Code: Select all

c:\Users\Marc\Desktop>findtest
raw wmic output:
Name
EP45-UD3L

piping to find limits what gets returned:
EP45-UD3L

using this is a FOR can set the result to a variable
model=
model=EP45-UD3L

Finally, a simple IF statement to check the model
---Don't Run Executable

c:\Users\Marc\Desktop>

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: Run exe if "model number"

#7 Post by aristosv » 13 Nov 2013 09:45

It's working, with a slight problem.
One of the laptop models is a DELL Latitude E6500, and the wmic command returns "Latitude E6500" which has a space in the name.

I tried adding quotations everywhere to resolve the issue, but no luck. The variable %model% simply outputs "Latitude"

Any ideas?

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run exe if "model number"

#8 Post by foxidrive » 13 Nov 2013 09:59

Use this:

FOR /F "delims=" %%i

Squashman
Expert
Posts: 4485
Joined: 23 Dec 2011 13:59

Re: Run exe if "model number"

#9 Post by Squashman » 13 Nov 2013 13:29

foxidrive wrote:Use this:

FOR /F "delims=" %%i

Always a good habit to get into.

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: Run exe if "model number"

#10 Post by aristosv » 14 Nov 2013 00:35

Yes, now the %model% variable is working :)

But now when I run

Code: Select all

IF %model%==Latitude E6500 (echo ---Run Executable) ELSE (echo ---Don't Run Executable)

I get

Code: Select all

E6500 was unexpected at this time.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run exe if "model number"

#11 Post by foxidrive » 14 Nov 2013 01:44

Double quotes fixes spaces in tests - but you might find that WMIC has appended a CR or spaces to the end of the line.

Code: Select all

IF "%model%"=="Latitude E6500" (echo ---Run Executable) ELSE (echo ---Don't Run Executable)

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Run exe if "model number"

#12 Post by Magialisk » 14 Nov 2013 01:44

Change it to IF "%model%"=="Latitude E6500" (echo ---Run Executable) ELSE (echo ---Don't Run Executable)

You need the quotes to handle the space, and you need them on both sides to maintain the equality.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run exe if "model number"

#13 Post by foxidrive » 14 Nov 2013 02:58

This code should get around the issues with Wmic

Note that single term model names will have a trailing space and that needs to be added to the IF test, and dual term names will not have a trailing space.
This code expects that every model will have at least one numeral in the descriptor.


Code: Select all

set model=
FOR /F "tokens=1,2" %%a IN ('wmic csproduct get name ^| findstr "[0-9]" ') DO set "model=%%a %%b"
echo model="%model%"
echo.
IF "%model%"=="4242RR7 "        (echo ---Run Executable) ELSE (echo ---Don't Run Executable)
IF "%model%"=="Latitude E6500" (echo ---Run Executable) ELSE (echo ---Don't Run Executable)

Post Reply