0

Wanted to get string=abc

set string=abc[Data]123[USER]Adam
set find=*[Data]

call set wanted=%%string:!find!=%%
echo %wanted%

set wanted=[Data]%wanted%
echo %wanted%

call set JOB_NAME=%%string:!wanted!=%%
echo %JOB_NAME%

output:
123[USER]Adam
[Data]123[USER]Adam
abc

Q1 Is there a easy way one line command to remove string to the right after a delimiter was found ?set find=*[Data] remove before the delimiter but not after delimiter.Tried to change the wild card to set find=[Data]* won't work at all.

Q2 Try to set string in this format set string=abc<Data>123<USER>Adam even with escape character set string=abc^<Data^>123^<USER^>Adamthe system prompt The system cannot find the file specified

RandyWong
  • 85
  • 1
  • 9
  • 1
    Please explain clearly what you are trying to do instead of leaving us to guess. For instance `this series of instructions` produced `these results` where I expected `these other results`. If you tell us what your ultimate goal is, we may devise another way of reaching it. – Magoo Nov 10 '16 at 04:32
  • @Magoo I did write on top asking how to get string 123 or remove string to the right if delimiter is found for example `abc123Adam` how you remove ADAM let say if you found . – RandyWong Nov 10 '16 at 04:48
  • @Magoo I added a few questions and also the change code above.The code works just that is not the format I want can help ? – RandyWong Nov 10 '16 at 06:11
  • Why wouldn't you just use a `FOR /F` command and specify the `DELIMS=` option with the delimiter you need to use. – Squashman Nov 10 '16 at 14:02

2 Answers2

2

The key, I believe, is quoting the strings

Still not clear WHAT you want to do, only how you've not been able to do it.

Try one or more of these, depending on your preference

@ECHO Off
SETLOCAL
set "string=abc<Data>123<USER>Adam"
set "find=*<Data>"

call set "wanted=%%string:%find%=%%"
SET wanted

set "wanted=<Data>%wanted%"
SET wanted

call set "JOB_NAME=%%string:%wanted%=%%"
SET JOB_NAME

ECHO =====================================
set "string=abc<Data>123<USER>Adam"
set "find=*<USER>"

call set "wanted=%%string:%find%=%%"
SET wanted

set "wanted=<USER>%wanted%"
SET wanted

call set "wanted=%%string:%wanted%=%%"
SET wanted

set "find=*<Data>"
call set "JOB_NAME=%%wanted:%find%=%%"
SET JOB_NAME

ECHO =====================================
set "string=abc<Data>123<USER>Adam"
set "find=*<USER>"

call set "JOB_NAME=%%string:%find%=%%"
SET JOB_NAME

ECHO =====================================

set "string=abc<Data>123<USER>Adam"
FOR /f "tokens=1-5delims=<>" %%a IN ("%string%") DO SET "string1=%%a"&SET "string2=%%b"&SET "string3=%%c"&SET "string4=%%d"&SET "string5=%%e"

SET string

GOTO :EOF
Magoo
  • 77,302
  • 8
  • 62
  • 84
2

Q1

I think you are talking about sub-string replacement -- correct me if I am wrong.
There is no reverse thing of %VARIABLE:*search:replac%, but there is a nice work-around, supposing the search part consists of a single character (# in the example below) and it does not appear as the first character in the string:

set "VARIABLE=test#string"
for /F "delims=# eol=#" %%F in ("%VARIABLE%") do echo(%%F

This returns everything in %VARIABLE% before the first occurrence of the # character. (The eol=# portion simply disables the default eol character ;.)

If the delimiter character might also appear at the first position in the string, precede a dummy character temporarily, other than the delimiter (like _, for instance):

set "VARIABLE=test#string"
for /F "delims=#" %%F in ("_%VARIABLE%") do (
    set "VALUE=%%F"
    setlocal EnableDelayedExpansion
    echo(!VALUE:~1!
    endlocal
)

If the search portion consists of multiple characters, things become a bit more complicated, but you could replace it by a single character that does not appear in the part before and use one of the above solutions.

Q2

Magoo already shows the best way to avoid such errors in his answer.

I want to show you why the escaping failed in your approach:
The escaping like ^< and ^> is consumed by setting the variable with set. When expanding it like %VARIABLE%, it appears unescaped, that is why you receive an error. But -- besides quotation -- there are work-arounds:

rem // This sets the variable to `abc<Data>123` literally:
set VARIABLE=abc^<Data^>123
rem // This fails as it expands to `abc<Data>123`:
echo(%VARIABLE%

setlocal EnableDelayedExpansion
rem /* This succeeds dueto delayed expansion, because
rem    special characters are no longer recognised then: */
echo(!VARIABLE!
endlocal

rem // This sets the variable to `abc^<Data^>123` literally:
set VARIABLE=abc^^^<Data^^^>123
rem /* This succeeds as it expands to `abc^<Data^>123`,
rem    hence the poisonous characters are escaped properly: */
echo(%VARIABLE%
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thanks for answering my question I understand the explanation in Q2 but for Q1 can provide a string example that can be use on your source code cause I can't really visualize .In shell I can remove a part of a string by just using this http://stackoverflow.com/questions/19482123/extract-part-of-a-string-using-bash-cut-split but in batch is kinda complicated .And I saw that it requires two step to remove string right hand side in the sub-string replacement link so I was wondering if there is an eazy way to do it – RandyWong Nov 10 '16 at 09:58
  • I just added test strings (Q1): `set "VARIABLE=test#string"`; the sub-string expansion syntax `echo(%VARIABLE:*#=%` returns `string`, the `for /F` solution returns `test`; is that what you need? – aschipfl Nov 10 '16 at 10:45