0
1
Let me explain what did I mean.
I run cmder.exe (via init.bat
) inside a cmd.exe console window. See the attached GIF. For this purpose I wrote a little batch script cmr.bat
shown below. It does work as it meant to.
@echo off
%comspec% /k "C:\cmder\vendor\init.bat %*"
EXIT /B %ERRORLEVEL%
But, this approach doesn't provide the ability to run the init.bat
in elevated mode directly.
Being inspired by the given solution, I created another batch file amr.bat
in order to run the inner init.bat
in elevated mode.
Here is the content of the amr.bat
:: Automatically check & get admin rights V2
@echo off
:: BatchGotAdmin
::-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::::::::::::::::::::::::::::::
:: START OF USER CUSTOM CODE
::::::::::::::::::::::::::::::
%comspec% /k "C:\cmder\vendor\init.bat %*" rem THIS IS MY CODE
EXIT /B %ERRORLEVEL%
The amr.bat
does run the init.bat
in the elevated mode, but doesn't keep (retain) the working directory from which it has been called. The below GIF screencast demonstrates this behavior.
The init.bat
didn't receive the working directory from which it was invoked. Instead init.bat
was started with the working directory set to the location of the amr.bat
(which resides in C:\BatScripts
directory).
So my question is, how can I modify the amr.bat
script, in order to keep (retain) the current directory from which it was invoked?
Any ideas?