Finding String in Variable Name
Moderator: DosItHelp
Finding String in Variable Name
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?
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
Code: Select all
echo("%computername%"|find "text">nul && call x
Re: Finding String in Variable Name
Another option.
Code: Select all
If %COMPUTERNAME:MyString=% NEq %COMPUTERNAME% 'Do Something'
Re: Finding String in Variable Name
Thank you both so much!
Re: Finding String in Variable Name
Where would I put my string in this command? "Mystring"? And then keep the equals after my search string?Compo wrote:Another option.Code: Select all
If %COMPUTERNAME:MyString=% NEq %COMPUTERNAME% 'Do Something'
Re: Finding String in Variable Name
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
Helps to see all the code you are using. We are not omniscient.
Re: Finding String in Variable Name
Squashman wrote:Helps to see all the code you are using.
+1
Re: Finding String in Variable Name
Okay, here's the full batch for each version of the solution posted in this thread. Any assistance appreciated.
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.

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.

Re: Finding String in Variable Name
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
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
I agree Compo!
Re: Finding String in Variable Name
I can't post the exact code due to security reasons. I've revised it to this and everything works correctly.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.
Code: Select all
echo("%computername%"|find "NEO">nul && GOTO MAP
GOTO NOMAP
:MAP
ECHO MAP WORKING!
GOTO END
:NOMAP
ECHO NO MAP WORKING!
:END
Re: Finding String in Variable Name
Thank you for the simplification of the algorithm. I'm going to use this simpler version.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

Re: Finding String in Variable Name
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?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
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