DelayedVariableExpansion

M

Michael.Knight

Hello world, especially Microsoft experts ...

I'm trying for at least two weeks long to find a cure for the AMAZING
:(((( delayed variable expansion feature. Almost all things do work in my
batch script, except one thing - EXCLAMATION MARK.

I have found one man's notes for bacth files and now I can echo
exclamation mark when delayed variable expansion is ON. But I cannot echo
the exclamation mark that is part of a variable:

*************************************************
setlocal enabledelayedexpansion

ECHO D:\^^!Backup

- this will show D:\!Backup on the screen => OK

*************************************************

setlocal enabledelayedexpansion
SET BackupDirRoot=D:\^^!Backup
ECHO %BackupDirRoot%

- this will show D:\Backup on the screen => BAD BAD BAD

*************************************************

The thing is that in the script I have to almost everytime disable and
enable delayedvariableexpansion feature, not talking about the thing to be
carefull where to use %variable% and where to use !variable!
:((((((((((((((((

If someone knows how to get out of this MS mess, I would be very gratefull
to him, but otherwise, people, you must understand that after almost a
month of searching on the net how to solve this and being nearly at the
same point, I cannot say that this feature is GOOD or better said: WELL
DONE !!! :((

"Michael Knight"
 
P

Pegasus \(MVP\)

Michael.Knight said:
Hello world, especially Microsoft experts ...

I'm trying for at least two weeks long to find a cure for the AMAZING
:(((( delayed variable expansion feature. Almost all things do work in my
batch script, except one thing - EXCLAMATION MARK.

I have found one man's notes for bacth files and now I can echo
exclamation mark when delayed variable expansion is ON. But I cannot echo
the exclamation mark that is part of a variable:

*************************************************
setlocal enabledelayedexpansion

ECHO D:\^^!Backup

- this will show D:\!Backup on the screen => OK

*************************************************

setlocal enabledelayedexpansion
SET BackupDirRoot=D:\^^!Backup
ECHO %BackupDirRoot%

- this will show D:\Backup on the screen => BAD BAD BAD

*************************************************

The thing is that in the script I have to almost everytime disable and
enable delayedvariableexpansion feature, not talking about the thing to be
carefull where to use %variable% and where to use !variable!
:((((((((((((((((

If someone knows how to get out of this MS mess, I would be very gratefull
to him, but otherwise, people, you must understand that after almost a
month of searching on the net how to solve this and being nearly at the
same point, I cannot say that this feature is GOOD or better said: WELL
DONE !!! :((

"Michael Knight"

I suspect you have stumbled over a limitation of batch
files using delayed variable expansion. Try not to use it -
AFAIK, you can always avoid it by calling subroutines.
You might also repost your question in alt.msdos.batch.nt.
This is where the batch file experts hang out. They would
love the challenge!
 
M

Michael.Knight

Hi,

When I do enable DelayedVariableExpansion feauture, the varibale is (in a
FOR cycle or IF command) not updated and the system does not know
!variable!, only %variable% :(
OK, I will post this issue at the suggested phorum and if I will know
more, I will keep you updated .

Many thanks !
 
P

Pegasus \(MVP\)

I am quite aware of this. That's why I suggested subroutines -
they are not affected by this issue because they do not
require !variables!.
 
M

Michael.Knight

I can't access alt.msdos.batch.nt server (is it a news server ?), even not
via internet browser. Even alt.msdos.batch site ...
Any help ? Has it gone ?

Thanks for the feedback

"Michael Knight"
 
P

Pegasus \(MVP\)

The usual way is to create a newsgroup account in
Outlook Express. You need to specify your IPS's
news server - it is usually something like news.MyISP.com.cz.
Ask your ISP if you cannot work it out. After specifying
the news server, you can select any of around 55,000
newsgroups, e.g. alt.msdos.batch.nt.

Remember that you can avoid delayed expansion issues,
by using subroutines. The two examples below do exactly
the same thing. Unfortunately the second example takes
about 200 times longer to run . . .

@echo off
setlocal EnableDelayedExpansion
set sum=0
for /L %%a in (1,1,100) do set /a sum=!sum! + %%a
echo Sum=%sum%
endlocal

@echo off
set sum=0
for /L %%a in (1,1,100) do call :Sub %%a
echo Sum=%sum%
goto :eof

:Sub
set /a sum=%sum% + %1


Michael.Knight said:
I can't access alt.msdos.batch.nt server (is it a news server ?), even not
via internet browser. Even alt.msdos.batch site ...
Any help ? Has it gone ?

Thanks for the feedback

"Michael Knight"

I am quite aware of this. That's why I suggested subroutines -
they are not affected by this issue because they do not
require !variables!.


Michael.Knight said:
Hi,

When I do enable DelayedVariableExpansion feauture, the varibale is (in
a
FOR cycle or IF command) not updated and the system does not know
!variable!, only %variable% :(
OK, I will post this issue at the suggested phorum and if I will know
more, I will keep you updated .

Many thanks !
 
M

Michael.Knight

Well, I know how to configure Outlook Express or any other News reader,
the only one thing I need is to know the right NEWS server IP. I'm in
Czech republic, we do not have some NEWS servers provided by our ISPs ...
AFAIK ... this is something we must discover ourselves, so as I have
discovered what is the MS news server ;)

I will try with the subroutines I knew before.

Thanks
"Michael Knight"
 
P

Pegasus \(MVP\)

If your ISP tells you that he does not run a news server then
I suggest that you use Google to look for links to this
newsgroup. Note that alt.msdos.batch.nt is NOT a Microsoft
newsgroup, hence you cannot access it through the
Microsoft news server.
 
M

Michael.Knight

Yes, I know ;) really ;)

Many thanks for your guidance you have provided here so far (Y).
And because I haven't found anything more and I don't have access to any
group I will use the subroutines ... I will try to use just the following
as well:

GOTO DelayedRouteine01
:DelayedRoutine01

.... and if that will not help, I will use the subroutines as you have
suggested.

"Michael Knight"
 
M

Michael.Knight

Ha, ha, it works ! :D

SetLocal DisableDelayedExpansion
Set VAR=before
if "%VAR%" == "before" (
@Set VAR=after
@if "%VAR%" == "after" @echo If you see this, it
worked
@ECHO This time the variable is NOT updated...
@ECHO var1 %VAR%
@ECHO exclamation !
@ECHO exclamation ^^! ... It works 100 %%
@ECHO.

this works ===> Goto DelayedRoutine01
this works ===> :DelayedRoutine01

@ECHO This time the variable IS updated...
@ECHO var1 %VAR%
@if "%VAR%" == "after" @echo If you see this, it
worked
@ECHO exclamation !
@ECHO exclamation ^^! ... It works 100 %%
@ECHO.
)

I just now have to test other functions that are dependant on
DelayedVariableExpansion.

Regards
"Michael Knight"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top