Page 1 of 2
Finding String in Variable Name
Posted: 06 Dec 2014 00:42
by Samir
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?
Re: Finding String in Variable Name
Posted: 06 Dec 2014 03:41
by foxidrive
Code: Select all
echo("%computername%"|find "text">nul && call x
Re: Finding String in Variable Name
Posted: 06 Dec 2014 10:38
by Compo
Another option.
Code: Select all
If %COMPUTERNAME:MyString=% NEq %COMPUTERNAME% 'Do Something'
Re: Finding String in Variable Name
Posted: 06 Dec 2014 12:23
by Samir
Thank you both so much!
Re: Finding String in Variable Name
Posted: 06 Dec 2014 12:29
by Samir
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?
Re: Finding String in Variable Name
Posted: 06 Dec 2014 12:30
by Samir
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?
Re: Finding String in Variable Name
Posted: 06 Dec 2014 13:14
by Squashman
Helps to see all the code you are using. We are not omniscient.
Re: Finding String in Variable Name
Posted: 07 Dec 2014 04:14
by foxidrive
Squashman wrote:Helps to see all the code you are using.
+1
Re: Finding String in Variable Name
Posted: 07 Dec 2014 10:14
by Samir
Okay, here's the full batch for each version of the solution posted in this thread. Any assistance appreciated.
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.
Re: Finding String in Variable Name
Posted: 07 Dec 2014 10:24
by Squashman
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.
Re: Finding String in Variable Name
Posted: 07 Dec 2014 10:50
by Compo
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
Re: Finding String in Variable Name
Posted: 07 Dec 2014 12:05
by Squashman
I agree Compo!
Re: Finding String in Variable Name
Posted: 09 Dec 2014 21:28
by Samir
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.
Re: Finding String in Variable Name
Posted: 09 Dec 2014 21:29
by Samir
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.

Re: Finding String in Variable Name
Posted: 09 Dec 2014 21:37
by Samir
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