Grief and code collapse from :- ECHO text )

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Grief and code collapse from :- ECHO text )

#1 Post by alan_b » 14 Jan 2012 16:50

I was surprised by the need for special action when outputting a closing bracket.
These work with ^escape or initial " :-

Code: Select all

  ECHO E(xit^) or-alternatively C(lose^)
  ECHO "E(xit) or-alternatively C(lose)

I am testing whether UAC intercepts creation and removal of %windir%\AdminTest,
and if it hinders then suggesting the user close and retry with "Run As Administrator"

This is the complete code from start to grief

Code: Select all

@echo off & setlocal EnableDelayedExpansion
CD /D %~dp0 & REM All files are here, not at stupid "Run As Administrator" default %windir%\
SET "UAC=%windir%\AdminTest" & IF EXIST !UAC! ( RD !UAC! & DEL /F /Q !UAC! )

MD !UAC! 2>NUL
RD !UAC! 2>NUL || (
  ECHO Suggest "Right Click" then "Run As Administrator" to avoid probable UAC GRIEF.
  ECHO E(xit^) or-alternatively C(lose^) 1
  ECHO "E(xit) or-alternatively C(lose) 2
  ECHO E(xit) or-alternatively C(lose) 3
  ECHO GOT TO HERE
)
PAUSE
GOTO :EOF

It crashes without any pause, thus :-

Code: Select all

E:\T\CCleaner\v313\T6>TT
or-alternatively was unexpected at this time.

E:\T\CCleaner\v313\T6>

It works properly when I remove the guilty line
ECHO E(xit) or-alternatively C(lose) 3

Code: Select all

E:\T\CCleaner\v313\T6>TT
Suggest "Right Click" then "Run As Administrator" to avoid probable UAC GRIEF.
E(xit) or-alternatively C(lose) 1
"E(xit) or-alternatively C(lose) 2
GOT TO HERE
Press any key to continue . . .

Is this a well documented "feature" that I should have known about ?

NB
I am also horrified that when I "Run As Administrator" the current drive and directory get switched to C:\Windows
I think there are now unwanted output files that should have gone to the folder
E:\T\CCleaner\v313\T6
and now they are swimming around in
C:\Windows.
Bad "Run As Administrator" for doing that to me.
I am glad Macrium partition images have "got my back"

Regards
Alan

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

Re: Grief and code collapse from :- ECHO text )

#2 Post by dbenham » 14 Jan 2012 17:30

I'm not sure anything is properly documented in the world of Windows batch. But it is certainly expected behavior.

If you have a begin block ( active, which you do, then any subsequent ) will close the block unless it is escaped or quoted.

If there is not an active open block, then your ECHO with the ) would not cause a problem.

Dave Benham

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Grief and code collapse from :- ECHO text )

#3 Post by alan_b » 14 Jan 2012 17:37

Thanks for the explanation.

All that and UAC aggro as well
If only IBM had chosen CP/M :)

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

Re: Grief and code collapse from :- ECHO text )

#4 Post by aGerman » 14 Jan 2012 19:28

Hi Alan.

The FSUTIL tool needs to run as admin. That could be an alternative instead of removing a folder which could exist before.

Code: Select all

@echo off &setlocal

echo %username%

:: maybe language dependent because of substring "admin"
for /f "tokens=*" %%a in ('net localgroup 2^>nul^|findstr /ic:"admin"') do (
  set "check=%%a"
  net user %username%|findstr /c:"%%a">nul &&(
    echo You belong to the local admins.
  )||(
    echo You don't belong to the local admins.
  )
)
if not defined check echo You have no access to run the NET command.

:: that's the part of interest (language independent)
fsutil fsinfo drives|findstr /c:":\\">nul &&(
  echo You work with elevated administrative privileges.
)||(
  echo You don't work with elevated administrative privileges.
)

pause>nul

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Grief and code collapse from :- ECHO text )

#5 Post by Ed Dyreen » 14 Jan 2012 20:32

'
I would like (no escaping required):

Code: Select all

@echo off &setlocal enableDelayedExpansion

(
  ECHO !"!Suggest "Right Click" then "Run As Administrator" to avoid probable UAC GRIEF.!"!
  ECHO !"!E(xit^) or-alternatively C(lose^) 1!"!
  ECHO "E(xit) or-alternatively C(lose) 2!"!
  ECHO !"!E(xit) or-alternatively C(lose) 3!"!
  ECHO !"!GOT TO HERE!"!
)

pause
exit
There also is the %%~ variation, Thanks jeb.
viewtopic.php?f=3&t=2593&start=0&hilit=virtual+quote

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Grief and code collapse from :- ECHO text )

#6 Post by alan_b » 15 Jan 2012 02:27

Thanks everyone.

I am not sure that Admin rights will be needed on XP which has no UAC aggravation,
so suspect a FSUTIL test would be needless for those systems.

NB I start by ensuring that %windir%\AdminTest is removed because I was given a code snippet that simply created and removed such a folder to detect UAC aggro.
Unfortunately the snippet failed to include a exit destination label and it crashed after creation but before removal.
When I found and fixed the code it no longer worked because the folder pre-existed and MD gave an error condition,
so until debugging is finished the initial precaution may be useful.

@ Ed
That is a nice idea I will find useful.
The implementation on all 5 lines is a bit overkill :)
I only had problems with the line concluding
C(lose) 3

Regards
Alan

Post Reply