-1

I have some data in a text file called mData.txt, data looks like this :

Master1:H1M1,H2M1

Master2:H1M2,H7M2

VMVPC092015:H1,H2,H3

DEKSTOP-UKUEA78:Machine1,Machine2

Master4:H1M4,H2M4


Here's some sample code from my batch file :

@echo off
setLocal enableDelayedExpansion

:: Set hostname
hostname.exe > __hName.tmp
set /p hNameVar=<__hName.tmp
del __hName.tmp
echo %hNameVar%

for /f "usebackq" %%i IN (`hostname`) do   set hNameVar2=%%i
echo %hNameVar2%

for /f "tokens=1,2 delims=:" %%a in (mData.txt) do (
    set mName=%%a
    set hName=%%b
    if !mName!==%hNameVar2% (
        echo FoundIt
        pause
    )else (
        echo here: !mName!
        echo there: !hNameVar2! 
    )
)

The name of my desktop is : DESKTOP-UKUEA78

I want this script to read the first machine name from each line in the text file and find out if that machine name is equal to the name of my desktop.

The execution is continuously going into else clause. I've attached a screenshot as well. hNameVar and hNameVar2 both are producing same o/p.ScreenShot This is my first batch project and I can't quite figure out where I'm going wrong. Would someone help me out please...

  • Usually not having a space between the `)` and the `else` will cause a syntax error. – Squashman Sep 04 '18 at 20:30
  • I guess there's something wrong with the way I'm comparing those values...adding a space did not help – Mandeep Singh Sep 04 '18 at 20:35
  • Probably a hidden space somewhere in one of the hostname commands. Try using `echo *%hNameVar%*` and `echo *%hNameVar2%*` to see if there's anything obviously different. – SomethingDark Sep 04 '18 at 22:41
  • Your whole script makes no seance, so you want to find & list the example: `Machine1,Machine2` next to the hostname? In your case `DESKTOP-UKUEA78`..? – John Kens Sep 05 '18 at 00:31
  • Machine names corresponding to hostname will be fetched and passed onto another script that is supposed launch all virtual machines, run specific processes on them etc etc...This is just sample test case for one specific scenario that I was stuck at.. – Mandeep Singh Sep 05 '18 at 01:11

1 Answers1

0

Alright I'm posting this with the few conditions you have in your post. I'm assuming you're trying to echo the results (Machines) next to your current machines host name. For example, If you're on DESKTOP-UKUEA78 and run the script, the script will return Machine1,Machine2.

Now lets get started, The way you're using > __hName.tmp is completely not needed as for /f "delims=" %%I in ('hostname') do set CurrentHost=%%I will grab your hostname and set it as an string.

For the process you're using to search the text document, there is a much more efficent way and thats using FindStr /I to search for all lines containing your current host name. This will return DEKSTOP-UKUEA78:Machine1,Machine2 if your hostname is DEKSTOP-UKUEA78. To get rid of the host name in front of the mechine name we can use simple syntax-replace.

The script bellow will ECHO the machine name's for the hostname it's ran on.

@ECHO OFF
setlocal enabledelayedexpansion

:: Get this PC's Host Name
for /f "delims=" %%I in ('hostname') do set CurrentHost=%%I

:: Grab lines that contain hostname only & extract machine name
for /f "tokens=1,2" %%G in ('FindStr /I "%CurrentHost%" "mData.txt" 2^>Nul') do (

set string=%%G
set string=!string:*:=%!

:: Echo Result - If two host of same type in file, will echo both.
echo Matching Machine Names For %CurrentHost%: !string!
echo(
)

pause

This script will return the following if ran on DESKTOP-UKUEA78:

Matching Machine Names For DESKTOP-UKUEA78: Machine1,Machine2
John Kens
  • 1,615
  • 2
  • 10
  • 28
  • Hey John...Thanks a lot, man...turns that I was just being a moron there were a few incorrect entries in the text file...though I certainly like your solution better...gonna try it out – Mandeep Singh Sep 05 '18 at 01:13