Solved

Batch file to find string in %COMPUTERNAME%

Posted on 2010-09-10
6
8,422 Views
Last Modified: 2013-04-04
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
Comment
Question by:graceout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 50 total points
ID: 33647226
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
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 75 total points
ID: 33647231
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
 
LVL 7

Expert Comment

by:BobintheNoc
ID: 33647281
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 7

Expert Comment

by:BobintheNoc
ID: 33647316
ooops, above example should have read:

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

I like the two previous answers better though, especially LEEW's.
0
 

Author Comment

by:graceout
ID: 33647373
Hey Billprew!

Pretty slick.

I can use this in a variety of ways!

Thanks!
0
 

Author Comment

by:graceout
ID: 33647444
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question