My UAC function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#16 Post by Adrianvdh » 09 Oct 2013 12:51

You extra computing code, does not make sense to me - sorry I am a retard? Could you explain what it does exactly and in details?
It could be handy some day ;)

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#17 Post by Adrianvdh » 09 Oct 2013 13:11

Sorry penpen, that does not work. Any other idea? (My batch file)

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#18 Post by Adrianvdh » 10 Oct 2013 13:26

Any one?
Image

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

Re: My UAC function

#19 Post by alan_b » 11 Oct 2013 13:37

Adrianvdh wrote:I need the technique to be like mine, the if errorlevel statement must be in the function.

And I need the use switches.

Sorry if old age has made me cynical,
but this sounds to me like a request for assistance with a school's homework exercise,
where the requirement is not to achieve a particular result,
but to demonstrate the correct syntax for using errorlevel and switches as taught in the last lesson. :)

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#20 Post by Adrianvdh » 12 Oct 2013 04:54

alan_b wrote:
Adrianvdh wrote:I need the technique to be like mine, the if errorlevel statement must be in the function.

And I need the use switches.

Sorry if old age has made me cynical,
but this sounds to me like a request for assistance with a school's homework exercise,
where the requirement is not to achieve a particular result,
but to demonstrate the correct syntax for using errorlevel and switches as taught in the last lesson. :)


NO! This they DO NOT teach at school. I am in Grade 9, I failed Grade 9 Last year so ya. Grade 10 we learn Java, etc.

I work on a personal project I work on in my own spare time (for the last year). But I can't really debug it until I fix the UAC function problem. So any idea anyone?

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

Re: My UAC function

#21 Post by aGerman » 12 Oct 2013 06:41

aGerman wrote:Try to write a simplified example (whole batch code) that shows the problem.

Regards
aGerman

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#22 Post by Adrianvdh » 12 Oct 2013 11:42

What do you mean. I have explained it enough haven't I? The problem is the errorlevel from the choice.exe is interfering with the UAC function.

How do I change this?

i really don't want to use "set /p" for user input on a menu.

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

Re: My UAC function

#23 Post by alan_b » 12 Oct 2013 14:36

Why on earth are you wasting time with code such as

Code: Select all

choice /C 12r /N /M "Make your selection: "
if errorlevel 3 cls & goto restart
if errorlevel 2 ( set mainmenuvar=Text1
goto mainmenuloginput )

That sort of code was appropriate when Windows 3.?? was a simple GUI that was run under the real operating system COMMAND.COM.

If you are writing code that is affected by User Access Control then your Batch commands are run by the CLI CMD.EXE running under the real operating system Windows Vista onwards.

CMD.EXE allows code such as

Code: Select all

set mylevel=%errorlevel%
choice /C 12r /N /M "Make your selection: "
if %mylevel%==3 (cls & goto restart)
if %mylevel%==2 ( set mainmenuvar=Text1
goto mainmenuloginput )


Just accept that many commands such as "choice" will affect %errorlevel%,
but you can use SET to allocate the current value of errorlevel to a "variable" which will remain fixed regardless of subsequent changes to errorlevel

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#24 Post by Adrianvdh » 12 Oct 2013 15:59

@alan_b:

Do I need to set var=%errorlevel% befor I call the UAC Function? because your code does not work :(

Still get the same problem:

I tried this:

Code: Select all

set choice=%errorlevel%
choice /C 123456itr /N /M "Make your selection: "
echo %choice%
pause


and the errorlevel returned as 0

Then I tried:

Code: Select all

choice /C 12 /N /M "Make your selection: "
echo %123%
echo %errorlevel%
pause
goto mainmenu
if "%errorlevel%"=="2" ( set mainmenuvar=Msg
goto mainmenuloginput )
if "%errorlevel%"=="1" ( set mainmenuvar=Msg
goto mainmenuloginput )
:mainmenuloginput
if "%logmode%"=="Enabled" call :checkfileUAC "/l" if "%logtype%"=="Detailed" echo [%time%] [%mainmenuvar%]>>"%logdir%"
if "%errorlevel%"=="2" goto Test2
if "%errorlevel%"=="1" goto Test1


After the goto mainmenuloginput is executed the errorlevel must mess up, and it does it return a 1 after the function is called. Any idea?

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

Re: My UAC function

#25 Post by aGerman » 12 Oct 2013 16:33

penpen wrote:

Code: Select all

choice /C 12 /N /M "Make your selection: "
set "errno=%errorlevel%"
:: ...

::replace: if errorlevel 2 goto Test2
if %errno% GEQ 2 goto Test2
::replace: if errorlevel 1 goto Test1
if %errno% GEQ 1 goto Test1

If you have to execute it in a subenvironment where delayed expansion is enabled then replace all percent signs with exclamation marks.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: My UAC function

#26 Post by penpen » 12 Oct 2013 19:13

Adrianvdh wrote:I tried this:

Code: Select all

set choice=%errorlevel%
choice /C 123456itr /N /M "Make your selection: "
echo %choice%
pause

and the errorlevel returned as 0

The errorlevel wasn't initialized, it could be anything.
You have to save the errorlevel into the choice variable AFTER you have executed choice(.exe) to get its returned errorlevel:

Code: Select all

choice /C 123456itr /N /M "Make your selection: "
set choice=%errorlevel%
echo %choice%
pause

Adrianvdh wrote:Then I tried:

Code: Select all

choice /C 12 /N /M "Make your selection: "
echo %123%
echo %errorlevel%
pause
goto mainmenu
if "%errorlevel%"=="2" ( set mainmenuvar=Msg
goto mainmenuloginput )
if "%errorlevel%"=="1" ( set mainmenuvar=Msg
goto mainmenuloginput )
:mainmenuloginput
if "%logmode%"=="Enabled" call :checkfileUAC "/l" if "%logtype%"=="Detailed" echo [%time%] [%mainmenuvar%]>>"%logdir%"
if "%errorlevel%"=="2" goto Test2
if "%errorlevel%"=="1" goto Test1

After the goto mainmenuloginput is executed the errorlevel must mess up, and it does it return a 1 after the function is called. Any idea?
If you store the errorlevel immidiately as i've suggested and use it later (errno, see post above) then its content would be that of the errorlevel after using choice.
Additionally i don't think the errorlevel messed up: I think the errorlevel is 1 because you have typed 1 at the choice prompt.

Btw: It seems, there is a typo: A missing && (or something like that) in the if line right after the :mainmenuloginput (between the call and the second if), which results in handling the if as parameters of the call, so they are never executed.

Note:
Adrianvdh wrote:You extra computing code, does not make sense to me - sorry I am a retard? Could you explain what it does exactly and in details?
It could be handy some day.
It computes the sum of all positive natural numbers up to %~2 (n):
1+2+3+4+5+...+...+(n-1)+n==n*(n+1)/2

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#27 Post by Adrianvdh » 13 Oct 2013 02:24

THANK YOU penpen I GOT IT TO WORK NOW. I AM SO HAPPY :) :) :)

So this is suitable:

Code: Select all

if "%logmode%"=="Enabled" if "%logtype%"=="Detailed" call :checkfileUAC "/l" && echo [%time%] [%mainmenuvar%]>>"%logdir%"

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

Re: My UAC function

#28 Post by alan_b » 13 Oct 2013 09:54

Adrianvdh wrote:@alan_b:
Do I need to set var=%errorlevel% befor I call the UAC Function? because your code does not work :(

My code does work - it merely failed to address your situation due to your inadequate specification.

You specified a function with label :checkfileUAC
That function seems to terminate with either an "Exit /b" or possibly "goto Errorwritefiles"

A bit later you have code that is unusable because there is no label or any indication of how it might be invoked.

You then seem to complain that CHOICE is having a bad effect upon errorlevel and always exits at the "first label:"

The first label at the top of the topic is :checkfileUAC
You have many more labels afterwards, including :mainmenuloginput.
It would have been useful to have specified the name of what you called the first label.

You did NOT show what code route led to the execution of CHOICE
and I assumed that you had done things that you expected to result in errorlevel having some particular value,
and that you had not realised that CHOICE is one of many commands that alter errorlevel and caused an exit to the un-named first label,
hence I pointed out that before you allowed CHOICE to change the value of errorlevel you could capture the current value of errorlevel with
SET mylevel=%errorlevel%

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: My UAC function

#29 Post by Adrianvdh » 13 Oct 2013 11:16

@alan_b

All I can say now is sorry for wasting your time, but the problem is fixed :) But anyway thanks :)

Post Reply