![]() |
I ran into this problem when working with symlinks on Windows 8.1 and then Windows 10. See Windows 10 symlinks.
The solution is pretty simple and it was tested and works on Windows 8.1 and Windows 10.
Note that scripts like this will eventually find their way somewhere into my git repository: https://github.com/spiralofhope/shell-random/tree/master
A batch file learning if it is run as administrator ∞
NET FILE 1>NUL 2>NUL & IF ERRORLEVEL 1 ( ECHO not admin ) ELSE ( ECHO admin )
Run a batch file only if administrator ∞
NET FILE 1>NUL 2>NUL & IF ERRORLEVEL 1 (EXIT /B) ECHO YES - This script __IS__ being run as the administrator
Run a batch file only if not administrator ∞
NET FILE 1>NUL 2>NUL & IF NOT ERRORLEVEL 1 (EXIT /B) ECHO NO - This script __IS NOT__ being run as the administrator
Elevate Privileges ∞
An example of use can be found on my github:
Put this code more-or-less at the beginning of your batch file:
Spoiler
:: ---- :: Elevate privileges :: ---- :: https://blog.spiralofhope.com?p=13553 :: https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator :: https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator/12264592#12264592 :checkPrivileges NET FILE 1>NUL 2>NUL if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) :getPrivileges if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges) ECHO. ECHO ************************************** ECHO Invoking UAC for Privilege Escalation ECHO ************************************** setlocal DisableDelayedExpansion set "batchPath=%~0" setlocal EnableDelayedExpansion ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" ECHO args = "ELEV " >> "%temp%\OEgetPrivileges.vbs" ECHO For Each strArg in WScript.Arguments >> "%temp%\OEgetPrivileges.vbs" ECHO args = args ^& strArg ^& " " >> "%temp%\OEgetPrivileges.vbs" ECHO Next >> "%temp%\OEgetPrivileges.vbs" ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" "%SystemRoot%\System32\WScript.exe" "%temp%\OEgetPrivileges.vbs" %* exit /B :gotPrivileges if '%1'=='ELEV' shift /1 setlocal & pushd . cd /d %~dp0 :: ----
BatchGotAdmin (Windows 8) ∞
- On Windows 10, as of 2016-01-31 this worked, but as of 2016-02-11 this no longer works.
-
I have not re-tested this code on Windows 8. It worked when I used it, some time ago.
- Perhaps the change to Windows 10 also means this no longer works on Windows 8. I don't know.
-
I am told that
cacls.exe
is is deprecated in Windows 7 and newer, and changingcalcs
toicalcs
works.- However, I've only ever used this as
cacls.exe
. - Perhaps this breaking in Windows 10 as of 2016-02-11 is because
calcs.exe
was removed.
- However, I've only ever used this as
Put this code more-or-less at the beginning of your batch file:
Spoiler
:: BatchGotAdmin :: https://blog.spiralofhope.com?p=13553 :: https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file :: https://sites.google.com/site/eneerge/scripts/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" :--------------------------------------
An example script to create a directory symlink ∞
Problem:
I want to have an application's user data (configuration) in a place of my choosing.
This example happens to be for Path of Exile - (2013 game).
- Create the directory
C:\Path_of_Exile
- Create the directory
C:\Path_of_Exile\_user data
-
Create the file
C:\Path_of_Exile\filename.cmd
with the below content:
Spoiler
@ECHO OFF SET SOURCE="%CD%\_user data" SET TARGET="C:\Users\user\Documents\My Games\Path of Exile" :: BatchGotAdmin :: https://blog.spiralofhope.com?p=13553 :: https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file :: https://sites.google.com/site/eneerge/scripts/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" :-------------------------------------- mklink /J %TARGET% %SOURCE%
TODO - Your source path can't have spaces in it. I don't know why.
An example script to create many symlinks ∞
Problem:
Given a directory which has many files and subdirectories, create symlinks in a companion directory.
- Create the directory
C:\source
- Create the directory
C:\source\one
- Create the directory
C:\source\two
- Create the directory
C:\target
-
Create the file
C:\source\filename.cmd
with the below content:
Spoiler
@ECHO OFF
SET "SOURCE=C:\source"
SET "TARGET=C:\target":: BatchGotAdmin
:: https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file
:: https://sites.google.com/site/eneerge/scripts/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":--------------------------------------
:: Directories
FOR /D %%i in ( *.* ) DO (ECHO * Processing %SOURCE%\%%i
ECHO %TARGET%\%%i
mklink /J "%TARGET%\%%i" "%SOURCE%\%%i")
:: Files
FOR %%i in ( * ) DO (ECHO * Processing %SOURCE%\%%i
ECHO %TARGET%\%%i
mklink "%TARGET%\%%i" "%SOURCE%\%%i")
This works at the commandline (when run as admin!) but not from explorer.exe if I run a filename.cmd
script with this:
SET "SOURCE=%CD%"
Last updated 2022-06-20 at 16:16:02