[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10275
  • Last Modified:

Batch file to find string in %COMPUTERNAME%

THis should be simple, but I'm trying to determine if a specific string exists in the Host Name during a login bat. script.

For example, all of the laptops in my site are named SITELTxxxxx so I need something that will do:

If %COMPUTERNAME% contains "LT" GOTO LT

Thanks in advance!

0
graceout
Asked:
graceout
2 Solutions
 
Bill PrewCommented:
How about this?  Basically, try and replace the chars you are looking for with nothing (so delete them) and if the strings are not equal after then it musy have contained the substring.

set cn=%COMPUTERNAME:LT=%
if "%cn%" NEQ "%COMPUTERNAME%" (
  echo Matched.
)

~bp
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
echo %computername% | findstr /i "LT" > nul
If "%errorlevel%"=="0" Goto LT



The above checks if the computer name contains "LT" (case insensitive) and suppresses any output.  Then it checks the error level - if errorlevel = 1, then it was NOT found, otherwise, it was.
0
 
BobintheNocCommented:
Using the FIND with /C command, and PIPING the result from the HOSTNAME values, and then Redirecting the output to a file will produce a result file that has either a 0 or a 1.  You can then check that file for either the 0 or 1 and move on in your script.

Example:
at a command prompt:

HOSTNAME | Find "LT" >result.txt

If the hostname (usuallly always the same as computername) contains LT, a file named result.txt will have a 1 inside.  If the hostname doesn't contain the letter combination of LT, a zero will be written to result.txt.

Probably not the most elegant way, but perhaps enough to get you to where you want to go?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BobintheNocCommented:
ooops, above example should have read:

HOSTNAME | find"LT" /c>result.txt

I like the two previous answers better though, especially LEEW's.
0
 
graceoutAuthor Commented:
Hey Billprew!

Pretty slick.

I can use this in a variety of ways!

Thanks!
0
 
graceoutAuthor Commented:
Thanks, all!

I think I'll be using leew's suggestion, but I'll definately using Billprew's syntax for other things.

Thanks again!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now