Run exe if "model number"
Moderator: DosItHelp
Run exe if "model number"
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
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
Re: Run exe if "model number"
You pipe the result through find or findstr, looking for the string, and then use the errorlevel to decide what to do.
Re: Run exe if "model number"
Would i be asking too much for an example?
Re: Run exe if "model number"
As it would seem to be a class assignment, show us what you think it should look like.
Re: Run exe if "model number"
It's not a class assignment 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
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...
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...
Re: Run exe if "model number"
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:
Results:
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)
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>
Re: Run exe if "model number"
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
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
Re: Run exe if "model number"
Use this:
FOR /F "delims=" %%i
FOR /F "delims=" %%i
Re: Run exe if "model number"
foxidrive wrote:Use this:
FOR /F "delims=" %%i
Always a good habit to get into.
Re: Run exe if "model number"
Yes, now the %model% variable is working
But now when I run
I get
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.
Re: Run exe if "model number"
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)
Re: Run exe if "model number"
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.
You need the quotes to handle the space, and you need them on both sides to maintain the equality.
Re: Run exe if "model number"
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.
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)