Truncate the string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kero
Posts: 16
Joined: 12 Jul 2016 00:49
Location: Moscow

Truncate the string

#1 Post by kero » 12 Jul 2016 01:20

Hi, DosTips !

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Truncate the string

#2 Post by ShadowThief » 12 Jul 2016 03:44

...what?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Truncate the string

#3 Post by aGerman » 12 Jul 2016 04:30

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

kero
Posts: 16
Joined: 12 Jul 2016 00:49
Location: Moscow

Re: Truncate the string

#4 Post by kero » 12 Jul 2016 04:56

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Truncate the string

#5 Post by Compo » 12 Jul 2016 07:48

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=^&:%

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Truncate the string

#6 Post by Aacini » 12 Jul 2016 08:15

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Truncate the string

#7 Post by Compo » 12 Jul 2016 09:20

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Truncate the string

#8 Post by aGerman » 12 Jul 2016 09:44

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Truncate the string

#9 Post by dbenham » 12 Jul 2016 10:17

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Truncate the string

#10 Post by aGerman » 12 Jul 2016 10:43

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

kero
Posts: 16
Joined: 12 Jul 2016 00:49
Location: Moscow

Re: Truncate the string

#11 Post by kero » 12 Jul 2016 10:57

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?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Truncate the string

#12 Post by Compo » 12 Jul 2016 11:30

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)

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Truncate the string

#13 Post by Aacini » 12 Jul 2016 12:36

Compo wrote:Because I saw something new I'd never seen before!

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

Antonio

kero
Posts: 16
Joined: 12 Jul 2016 00:49
Location: Moscow

Re: Truncate the string

#14 Post by kero » 13 Jul 2016 21:55

@Compo - clear, thanks.

Post Reply