Finding String in Variable Name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Finding String in Variable Name

#1 Post by Samir » 06 Dec 2014 00:42

I need to search the variable COMPUTERNAME displayed by the SET command and then if my string is found, have it do X.

I know there's an easy way to do this, but it's late and I can't remember.

Any assistance?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding String in Variable Name

#2 Post by foxidrive » 06 Dec 2014 03:41

Code: Select all

echo("%computername%"|find "text">nul && call x

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding String in Variable Name

#3 Post by Compo » 06 Dec 2014 10:38

Another option.

Code: Select all

If %COMPUTERNAME:MyString=% NEq %COMPUTERNAME% 'Do Something'

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#4 Post by Samir » 06 Dec 2014 12:23

Thank you both so much!

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#5 Post by Samir » 06 Dec 2014 12:29

Compo wrote:Another option.

Code: Select all

If %COMPUTERNAME:MyString=% NEq %COMPUTERNAME% 'Do Something'
Where would I put my string in this command? "Mystring"? And then keep the equals after my search string?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#6 Post by Samir » 06 Dec 2014 12:30

One odd thing that I can't figure out. If I put either of these in a batch file (first line actually) with a "goto map" as the command at the end, all I get is a new instance of the command prompt under xp. Any ideas?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Finding String in Variable Name

#7 Post by Squashman » 06 Dec 2014 13:14

Helps to see all the code you are using. We are not omniscient.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding String in Variable Name

#8 Post by foxidrive » 07 Dec 2014 04:14

Squashman wrote:Helps to see all the code you are using.

+1

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#9 Post by Samir » 07 Dec 2014 10:14

Okay, here's the full batch for each version of the solution posted in this thread. Any assistance appreciated. 8)

Code: Select all

echo("%computername%"|find "NEO">nul && GOTO MAP
:MAP
MAPS DRIVE
RUNS PROG
GOTO END
:NOMAP
RUNS PROG
:END

Code: Select all

If %COMPUTERNAME:NEO=% NEq %COMPUTERNAME% GOTO MAP
:MAP
MAPS DRIVE
RUNS PROG
GOTO END
:NOMAP
RUNS PROG
:END

Both results in the same odd result of a new command prompt just opening. :?: I'm definitely puzzled as both search lines aren't anything special--just a find with an && and a if statement with a comparison.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Finding String in Variable Name

#10 Post by Squashman » 07 Dec 2014 10:24

This is still mostly pseudo code. If you are going to obfuscate the code and data don't expect to get an answer to your problem.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding String in Variable Name

#11 Post by Compo » 07 Dec 2014 10:50

Samir wrote:

Code: Select all

If %COMPUTERNAME:NEO=% NEq %COMPUTERNAME% GOTO MAP
:MAP
MAPS DRIVE
RUNS PROG
GOTO END
:NOMAP
RUNS PROG
:END

If that really is the full batch then what's wrong with:

Code: Select all

If %COMPUTERNAME:NEO=% Equ %COMPUTERNAME% GOTO NOMAP
MAPS DRIVE
:NOMAP
RUNS PROG

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Finding String in Variable Name

#12 Post by Squashman » 07 Dec 2014 12:05

I agree Compo!

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#13 Post by Samir » 09 Dec 2014 21:28

Squashman wrote:This is still mostly pseudo code. If you are going to obfuscate the code and data don't expect to get an answer to your problem.
I can't post the exact code due to security reasons. I've revised it to this and everything works correctly.

Code: Select all

echo("%computername%"|find "NEO">nul && GOTO MAP
GOTO NOMAP
:MAP
ECHO MAP WORKING!
GOTO END
:NOMAP
ECHO NO MAP WORKING!
:END
I guess I'm on my own to figure out the rest.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#14 Post by Samir » 09 Dec 2014 21:29

Compo wrote:
Samir wrote:

Code: Select all

If %COMPUTERNAME:NEO=% NEq %COMPUTERNAME% GOTO MAP
:MAP
MAPS DRIVE
RUNS PROG
GOTO END
:NOMAP
RUNS PROG
:END

If that really is the full batch then what's wrong with:

Code: Select all

If %COMPUTERNAME:NEO=% Equ %COMPUTERNAME% GOTO NOMAP
MAPS DRIVE
:NOMAP
RUNS PROG
Thank you for the simplification of the algorithm. I'm going to use this simpler version. 8)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding String in Variable Name

#15 Post by Samir » 09 Dec 2014 21:37

Compo wrote:If that really is the full batch then what's wrong with:

Code: Select all

If %COMPUTERNAME:NEO=% Equ %COMPUTERNAME% GOTO NOMAP
MAPS DRIVE
:NOMAP
RUNS PROG
I spoke too soon. I forgot that the mapping must be undone after the program execution, hence the two branches. Right now I have this batch, but it's executing the map regardless. What may be wrong?

Code: Select all

@echo off
If %COMPUTERNAME:NEO=% NEq %COMPUTERNAME% GOTO MAP
:MAP
ECHO MAPS DRIVE
ECHO RUNS PROG
ECHO UNMAPS DRIVE
GOTO END
:NOMAP
ECHO RUNS PROG
:END

Post Reply