0

strong textscript.bat placed on the sendTo folder(file setted running as admin from properties -> advanced -> run as admin)

@echo off

cd %~dp1

echo %~dp1

echo "%~1"

pause

if i pass a parameters to this file with right click-->sendto--> script.bat for example a file superuser acc.txt the script, works, but if i run the script as admin, a cmd windows showup for a second and dont work.

How can i run this script as admin? after this snippet of code, there is a command that require admin privilege.

edit #1 i think the problem is related to the filename or pathname with space inside like "c:\path to space\" or "file with space.txt" and running the script as administrator.

2 Answers 2

0

Create a shortcut to your batch file in the shell:sendto folder and change its properties as follows:

  • right click -> ˙Properties˙ -> Shortcut tab;
  • Target -> cmd /C "D:\bat\SU\931003.bat";
  • Advanced button -> Run as administrator.

Note the first echo "%CD%" in next code snippet shows the initial working directory of your script ("C:\Windows\system32" if launched by SendTo feature):

==>type D:\bat\SU\931003.bat
@echo off
echo initial working directory "%CD%"
pushd "%~dp1"
echo current working directory "%CD%"
echo paramer #1 supplied "%~1"
echo all paramers %*
net session
pause

batch shortcut in the SendTo folder (to be run as administrator)

Sample output

initial working directory "C:\Windows\system32"
current working directory "D:\bat\1 & 2"
paramer #1 supplied "D:\bat\1 & 2\931003 test.txt"
all paramers "D:\bat\1 & 2\931003 test.txt" "D:\bat\1 & 2\batchfile.bat"
There are no entries in the list.

Press any key to continue . . .

Above output shows:

  • works with slightly unusual path and file name containing not only spaces but even & ampersand character...
  • works as administrator as otherwise the net session command returns System error 5 has occurred and Access is denied error messages.
3
  • hi, i have tryed your code. If i have a filename o pathname like "long path" with space inside, with administrator checked option dont work. the batch script is in d:\batch\run.bat, my OS is in c:
    – Frontender
    Commented Jun 22, 2015 at 20:10
  • Important: your shortcut target in the shell:SendTo folder should be cmd /C d:\batch\run.bat (note that windows will translate it to C:\Windows\System32\cmd.exe /C d:\batch\run.bat automatically after clicking Apply)
    – JosefZ
    Commented Jun 22, 2015 at 20:23
  • thanks a lot, seem work right now. i had find an issue. If the pathname of the directory with the .bat script look like "batch script inside", the script dont work. It's not a real problem, just use a simply name like "batch".
    – Frontender
    Commented Jun 22, 2015 at 20:59
0

You can use the runas command as follows:

runas /user:username program

You can also use something like elevate, a Command-Line UAC Elevation Utility, specifically for the command that requires admin privileges:

elevate [(-c | -k) [-n] [-u]] [-w] command

Options:
  -c  Launches a terminating command processor; equivalent to "cmd /c command".
  -k  Launches a persistent command processor; equivalent to "cmd /k command".
  -n  When using -c or -k, do not pushd the current directory before execution.
  -u  When using -c or -k, use Unicode; equivalent to "cmd /u".
  -w  Waits for termination; equivalent to "start /wait command".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .