Truncate the string
Moderator: DosItHelp
Truncate the string
Hi, DosTips !
Is it possible to use "&:" instead of "&rem." for truncate the string ?
Is it possible to use "&:" instead of "&rem." for truncate the string ?
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Truncate the string
...what?
Re: Truncate the string
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).
The following will cause a syntax error though:
Regards
aGerman
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
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.
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
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
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
Interesting!
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
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
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
I agree. Nothing that I even noticed before.
Output:
Regards
aGerman
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
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
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
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?
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
Because I saw something new I'd never seen before!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?
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
Compo wrote:Because I saw something new I'd never seen before!
Hi Compo, have you seen this crazy thread?
Antonio
Re: Truncate the string
@Compo - clear, thanks.