Solved

Batch file to find string in %COMPUTERNAME%

Posted on 2010-09-10
6
7,554 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
6 Comments
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:BobintheNoc
Comment Utility
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
Comment Utility
Hey Billprew!

Pretty slick.

I can use this in a variety of ways!

Thanks!
0
 

Author Comment

by:graceout
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now