Solved

Batch file to find string in %COMPUTERNAME%

Posted on 2010-09-10
6
8,638 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 96

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Independent Software Vendors: 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!

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 …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

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