[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Batch file to find string in %COMPUTERNAME%

Posted on 2010-09-10
6
Medium Priority
?
9,852 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 57

Assisted Solution

by:Bill Prew
Bill Prew earned 200 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 96

Accepted Solution

by:
Lee W, MVP earned 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

656 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