Page 1 of 1

Truncate the string

Posted: 12 Jul 2016 01:20
by kero
Hi, DosTips !

Is it possible to use "&:" instead of "&rem." for truncate the string ?

Re: Truncate the string

Posted: 12 Jul 2016 03:44
by ShadowThief
...what?

Re: Truncate the string

Posted: 12 Jul 2016 04:30
by aGerman
I guess he tries to concatenate a comment/remark with any other command.

First of all a single colon marks a label. Use a double colon instead.
You probably can concatenate comments using :: as long as you don't use it in a command block (that is enclosed in parentheses).

Code: Select all

@echo off
echo abc&:: xyz
pause


The following will cause a syntax error though:

Code: Select all

@echo off
for %%i in (1 2) do (
  echo abc&:: xyz
)
pause

Regards
aGerman

Re: Truncate the string

Posted: 12 Jul 2016 04:56
by kero
for clarity:

set str1=12345qwerty67890
set str2=%str1:er=&rem.%
set str3=%str1:er=&:%

these concrete str2 and str3 are the same, my question was: but is this always so?

Thanks to aGerman for counterexample.

Re: Truncate the string

Posted: 12 Jul 2016 07:48
by Compo
It is only seeing up to the ampersand, (&), character as it has a special meaning, most characters after that ampersand would act similarly.

You'd need to escape it if you wanted to replace with the ampersand

Code: Select all

set str1=12345qwerty67890
set str2=%str1:er=^&rem.%
set str3=%str1:er=^&:%

Re: Truncate the string

Posted: 12 Jul 2016 08:15
by Aacini
Interesting!

Code: Select all

echo on

echo This works&rem.This is omitted...

echo This works&:This disappear...

When the Batch file run, the entire "&:This disappear..." part just disappear!

I never have seen this before, but I suppose that the parsing rules explained elsewhere by Dave and jeb should explain this behavior.

Antonio

Re: Truncate the string

Posted: 12 Jul 2016 09:20
by Compo
Never noticed this myself before either

Code: Select all

C:\Users\Compo>set str1=12345qwerty67890

C:\Users\Compo>set str
str1=12345qwerty67890

C:\Users\Compo>set str2=%str1:*er=%

C:\Users\Compo>set str
str1=12345qwerty67890
str2=ty67890

C:\Users\Compo>set str3=%str1:er=&::%

C:\Users\Compo>set str
str1=12345qwerty67890
str2=ty67890
str3=12345qw

Re: Truncate the string

Posted: 12 Jul 2016 09:44
by aGerman
I agree. Nothing that I even noticed before.

Code: Select all

@echo off &setlocal
set "str1=xxIIyyIIzz"
set "str2=%str1:II=&rem.%"
set "str3=%str1:II=&:%"
set "str4=%str1:II=&::%"
set "str5=%str1:II=&%"
set str6=%str1:II=&rem.%
set str7=%str1:II=&:%
set str8=%str1:II=&::%
set str9=%str1:II=&%

set str
pause

Output:

Code: Select all

Der Befehl "yy" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Der Befehl "zz" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
str1=xxIIyyIIzz
str2=xx&rem.yy&rem.zz
str3=xx&:yy&:zz
str4=xx&::yy&::zz
str5=xx&yy&zz
str6=xx
str7=xx
str8=xx
str9=xx
Drücken Sie eine beliebige Taste . . .


Regards
aGerman

Re: Truncate the string

Posted: 12 Jul 2016 10:17
by dbenham
aGerman wrote:First of all a single colon marks a label. Use a double colon instead.
You probably can concatenate comments using :: as long as you don't use it in a command block (that is enclosed in parentheses).

The only reason for using :: within a batch script instead of : is so that it cannot function as a useable label.

Since the : within the expansion expression that is physically in the file cannot work as a label, there is no need to use ::.

Aacini wrote:I never have seen this before, but I suppose that the parsing rules explained elsewhere by Dave and jeb should explain this behavior.

Nor have I seen it before.

I don't think jeb has accounted for parsing of labels within his batch parser phases. I believe the label parsing occurs in phase 2 around the same time that REM is parsed. But I am not sure.


Dave Benham

Re: Truncate the string

Posted: 12 Jul 2016 10:43
by aGerman
Since the : within the expansion expression that is physically in the file cannot work as a label, there is no need to use ::.

I didn't think about that. Of course you're right.

Regards
aGerman

Re: Truncate the string

Posted: 12 Jul 2016 10:57
by kero
Hi Compo,

you wrote
"str2=%str1:*er=%"
instead of
"str2=%str1:er=&rem.%"
and got
"str2=ty67890"
instead of
"str2=12345qw"...

I don't understand - what for?

Re: Truncate the string

Posted: 12 Jul 2016 11:30
by Compo
kero wrote:Hi Compo,

you wrote
"str2=%str1:*er=%"
instead of
"str2=%str1:er=&rem.%"
and got
"str2=ty67890"
instead of
"str2=12345qw"...

I don't understand - what for?
Because I saw something new I'd never seen before!

I recreated what your example was doing at str3 instead! (remove all characters from the existing variable including and after er)

We've probably now determined that the effect your example is having is the same as "str2=%str1:er=&:%"

What I created at str2 was the known method of achieving the opposite, (remove all characters from the existing variable up to and including er)

Re: Truncate the string

Posted: 12 Jul 2016 12:36
by Aacini
Compo wrote:Because I saw something new I'd never seen before!

Hi Compo, have you seen this crazy thread? :mrgreen:

Antonio

Re: Truncate the string

Posted: 13 Jul 2016 21:55
by kero
@Compo - clear, thanks.