I was wondering whether someone could simply explain to me how to Add and Remove specific keywords/text from a Variable in batch...
You can use text replacement: set var=%var:foo=% to remove foo from %var%.
Environment variable substitution has been enhanced as follows:
%PATH:str1=str2%
would expand the PATH environment variable, substituting each
occurrence of "str1" in the expanded result with "str2". "str2" can
be the empty string to effectively delete all occurrences of "str1"
from the expanded output. "str1" can begin with an asterisk, in which
case it will match everything from the beginning of the expanded
output to the first occurrence of the remaining portion of str1.
To add things to an environment variable you can either add it at the beginning or end:
set "var=beginning %var%"
set "var=%var% end"
or muck around with substrings if you need something in the middle:
set "var=%var:~0,5% middle %var:~5%"
Related
So I need to see if a string contains something. I was thinking, you know how you can do set VAR=%VAR:-=_% to target certain things inside the variable and change them with the set command. Well, is there a way I can do this with "IF" statements?.
Example of what it might look like:
if %VAR%==%VAR:word% echo yes
This command doesn't actually work but if it did, it would look to see if %VAR% contains "word" anywhere in it.
You're very close, but you've only got half of a valid string substitution command, which isn't causing a syntax error, but it is causing a flow in the logic: right now you're comparing %VAR% to a variable that the interpreter can't access.
If you use string substitution to remove the word you're looking for and then compare that to the original variable, you can use an if not statement to determine if the variable was updated by the substitution.
if not "%var%"=="%var:word=%" echo yes
I have a batch variable called version with this value "2930.2323 "
Now i want to remove the last character (or all spaces). I've tried both ways but the whitespace is not removed.
// MYVAR is set at the beginning of my batch file by another source code
SET "MYVAR=%MYVAR: =%"
Your code doesn't remove "Whitespaces", but SPACEs only (ie if it's a TAB it won't be removed unless you add another line to remove TABs too).
For your given string, I suggest another approach:
for %%a in (%myvar%) do set "myvar=%%a"
echo --%myvar%--
Note: that only works for whitespaces at the beginning or end of the string. A space in the middle of the string will split the string into two (or more). Also, some special characters will be problematic. But it will work with strings like your example ("Version numbers")
I am dealing with some code that was put together by someone who has long since left the company. It reads:
REM XX.XXX YYYYMMDD Author Description
REM version=4.3 &:20170418 comment comment comment
REM version=4.4 &:20170519 comment comment comment
SET version=4.5c &:20170604 comment comment comment
SET "version=%version: =%"
After puzzling through this, we finally figured out two things: one, that the & thing works to tell DOS that a new command is coming in the same line, and then the :date just gets thrown out because DOS doesn't know what to do with it.
But then we get to this SET "version=%version: =%" nonsense.
All I've been able to deduce from it so far is that it will remove spaces, so that if I did this instead:
SET version=4.5 c
SET "version=%version: =%"
ECHO %version%
I'll get "4.5c" echoed to the screen.
I can't find any information about this ": =%" business anywhere online. Is there a good reason to be doing this?
What Is Going On?:
This looks like Variable Edit/Replace or in other terms syntax-replacement. What this allows you to do is take a string, and replace characters or words from it and either replace the existing string or create a new modified one.
Taking example SET "version=%version: =%" This will be modifying the string version and removing all spaces from the string.
Positives To This Method:
Being that some strings or code need to be modified, you can very conveniently use a pure batch option to replace words in text files, remove words from string, add commas after words, and even remove the last x characters in a string.
syntax-replacement is commonly used for issues that that cannot be solved within a for loop or strings that need to be tweaked before being used, an example will be folder paths. In for loops, when processing strings containing \ and trying to use the delims=\, you sometimes need to change it to a less conflicting character as ; - SET "String=%String:\=;". The uses are endless.
Negatives To This Method:
This is a very easy way to edit strings but can come with a negative being that you cannot edit strings with special characters without first using an ^ to escape special characters in the base string. An Example of this will be the following:
SET "version=Hello & There" - This will break the syntax-replacement code as &
is calling a new command.
SET "version=Hello ^& There" - This is the proper way to "ignore" the & symbol
for processing.
Check out Set /? in a CMD window for more information.
I have a string like this:
Testing:"abc"def"ghi"
I want to get: abc"def"ghi and put it into a variable, is there any possible way?
I want a general way of doing that (first and last quotes, not first and fourth quotes)
This will work reliably as long as the string does not contain unquoted special characters like & | > < ^
#echo off
set str=Testing:"abc"def"ghi"extra
echo str = %str%
set "new=%str:*"=%
echo new = %new%
-- OUTPUT --
str = Testing:"abc"def"ghi"extra
new = abc"def"ghi
Explanation
There are two parts to this solution, both in the same line of code.
1) Remove all characters up through the first ".
This part uses the documented search and replace feature of variable expansion, with an asterisk before the search string. The following is an excerpt from the help gotten by typing HELP SET or SET /? from the command prompt.
Environment variable substitution has been enhanced as follows:
%PATH:str1=str2%
would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2". "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output. "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.
2) Find the last occurrence of " and truncate the string at that point.
The entire SET assignment expression can be enclosed in quotes, and the enclosing quotes will be discarded and all text after the last quote ignored. The statement below will define variable var to have a value of value.
set "var=value" this text after the last quote is ignored
If there is no last quote, then the entire remainder of the line is included in the value, possibly with hidden spaces.
set "var=This entire sentence is included in the value.
I am not aware of any official documentation for this feature, but it is an important tool for batch file development.
This process occurs after the expansion from part 1 has completed. So the the SET truncates at the last occurrence of " in the expanded value.
I am a windows batch dunce.
I have a variable storing some text surrounded by hard brackets like:
[glcikLhvxq1BwPBZN0EGMQ==]
But I need to pass it as an argument like:
glcikLhvxq1BwPBZN0EGMQ==
How can I strip these hard brackets from the beginning and end in my windows batch file?
You can use the sub-string syntax:
set foo=[glcikLhvxq1BwPBZN0EGMQ==]
set foo2=%foo:~1,-1%
which will remove the first and last characters. The sub-string starts here at the second character (so 1, zero-based) and extends until the second-to-last character (-1).
This is detailed more thoroughly in help set.