Reputation: 25
I have a text file that I want to check that a string does not contain a "=" symbol. I am finding it difficult to figure out how to set "=" to a variable in BATCH script.I have tried set MDSSTR=%%= and ="%%=" to no avail
An example of what I am trying to do is below:
//example
set MDSStr="="
set mdsvar="Hello=World"
if NOT %mdsvar% == %MDSStr% ( Do Something)
Does anyone know how to make a "=" symbol become a string? Thank you in advance.
Upvotes: 1
Views: 3518
Reputation: 80033
@ECHO OFF
SETLOCAL
set "MDSStr=="
set "mdsvar=Hello=World"
set "mdsvar2==Hello World"
set "mdsvar3=Hello World="
set "mdsvar4=Hello=====World"
set "mdsvar5======Hello World"
set "mdsvar6=Hello World====="
set "mdsvar7=Hello World"
SET mds
ECHO --------------------------------------------------------------------
FOR /f "tokens=1*delims==" %%a IN ("x%mdsstr%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar2%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar3%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar4%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar5%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar6%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
FOR /f "tokens=1*delims==" %%a IN ("x%mdsvar7%x") DO IF "%%b"=="" (ECHO no "=") ELSE (ECHO "=" found)
GOTO :EOF
This demonstrates a method of detecting the presence of =
in a string.
the variables starting mds
are set up to various combinations of characters containing =
- some at the start, some at the end, some in the middle. The very last variable is set to not contain =
.
The values set in the mds
variables are then shown and each is processed for a report.
Unfortunately, you seem to explain how you want to do something, but not what it is that you want to do. Perhaps it's simply find out whether the string contains "="
, but that seems to be overshadowed by the quest to have a variable with value =
Upvotes: 3
Reputation: 4750
To put an "=" in a string do this:
set "MDSStr=="
Echo your string and pipe the output to the find command to find display only lines that do not contain the find string (=) like this:
set "MDSStr=="
set mdsvar="Hello=World"
echo(%mdsvar% | find /v "%MDSStr%"
Upvotes: 0