0

Starting my batch-file looks like this:

start "" "%PROGRAMFILES%\program.exe"

If i am putting there a %1 at the end:

start "" "%PROGRAMFILES%\program.exe" %1

, i am able to open a non-specifif file, if i change "always open with" and link this to my batch-file.

But there is a problem - i would like to run my program as an administrator. If i create a link to my programm, give this link admin-rights and define that my filetype-ending should be opend with the "link to the program" it completely ignores the admin-rights.

Therefore i found an other solution:

:: 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 "%PROGRAMFILES%\program.exe", "/c %~s0 
%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin
pushd "%CD%"
CD /D "%~dp0"

With the help of this code my batch-file runs as administrator and the program starts, but the file doesn´t open in window. If tried several things with adding a "%1", but it doesn´t work.

Any idea?

Thanks for Help!

Mofi
  • 46,139
  • 17
  • 80
  • 143
Nils
  • 409
  • 6
  • 16
  • What about `"%~1"`? – aschipfl Jun 18 '21 at 07:54
  • right behind: UAC.ShellExecute "%PROGRAMFILES%\program.exe" "%~1" it doesnt work. :/ – Nils Jun 18 '21 at 08:25
  • 1
    My answer on [Can't run as Admin](https://stackoverflow.com/a/41493926/3074564) contains all the details to know to make your batch file working. However, a batch file is not needed at all for the purpose you want to achieve. Browse in Windows File Explorer to the file `%PROGRAMFILES%\program.exe`, right click on it and click in submenu __Send to__ of the context menu on item __Desktop (create shortcut)__. Then make your desktop visible which contains now a shortcut file (`.lnk` file) with name `program.exe`. Select the shortcut file, press Ctrl+X and open in Windows File Explorer the folder: – Mofi Jun 18 '21 at 12:46
  • 2
    `%APPDATA%\Microsoft\Windows\SendTo` and paste the shortcut file with Ctrl+V into this folder. Then press key __F2__ and rename the file from `program.exe` to whatever you like to see in future in the __Send to__ context submenu. The file extension `.lnk` must be kept. Next right click on the shortcut file and click in context menu on last item __Properties__. On tab __Shortcut__ click on button __Advanced__ and check the option __Run as administrator__. Press twice on button __OK__ and the __Send to__ context menu item is ready for usage to run the program with clicked file as administrator. – Mofi Jun 18 '21 at 12:52
  • @Mofi Works like a sharm. :) You don´t know any other way how i can arrange that thing with double clicking on the file? – Nils Jun 18 '21 at 17:19
  • If `%PROGRAMFILES%\program.exe` should be always executed as administrator independent on how it is started without or with a file name as argument, then browse in Windows File Explorer to this file, right click on the executable to open the context menu, left click on last item __Properties__, select tab __Compatibility__, check the option __Run this program as an administrator__ and click on button __OK__. – Mofi Jun 19 '21 at 10:32

0 Answers0