Page 1 of 1

Create symlink without admin mode

Posted: 07 May 2025 05:22
by kiwichick
Is there any way to create a symlink without having XY run as administrator?

Re: Create symlink without admin mode

Posted: 07 May 2025 10:18
by Horst
No, that's a Windows restriction..
Unfortunately, XY doesn't just ask using a UAC prompt for this operation
which would be a big improvement.

Re: Create symlink without admin mode

Posted: 07 May 2025 18:32
by admin
Will be possible with multi-threading.

Re: Create symlink without admin mode

Posted: 08 May 2025 03:47
by kiwichick
admin wrote: 07 May 2025 18:32 Will be possible with multi-threading.
Thanks, Don, that's good to know. Is that something that will be available with XYplorerTB?

Re: Create symlink without admin mode

Posted: 08 May 2025 03:50
by kiwichick
Horst wrote: 07 May 2025 10:18 No, that's a Windows restriction..
Unfortunately, XY doesn't just ask using a UAC prompt for this operation
which would be a big improvement.
Thanks, I realised that when I tried to create a symlink using command prompt and found it had to be run as administrator. So, is it possible to use XY to create a script that can run the command prompt as administrator and execute mklink code?

Re: Create symlink without admin mode

Posted: 08 May 2025 09:13
by admin
kiwichick wrote: 08 May 2025 03:47
admin wrote: 07 May 2025 18:32 Will be possible with multi-threading.
Thanks, Don, that's good to know. Is that something that will be available with XYplorerTB?
Yes, not immediately but it's planned.

Re: Create symlink without admin mode

Posted: 08 May 2025 11:55
by noembryo
kiwichick wrote: 07 May 2025 05:22 Is there any way to create a symlink without having XY run as administrator?
Without knowing any context on this question, one answer might be: "By using the Link Shell Extension"

Re: Create symlink without admin mode

Posted: 09 May 2025 10:53
by kiwichick
noembryo wrote: 08 May 2025 11:55 Without knowing any context on this question
I don't know what 'context' you want but XY scripting can create a symlink. However, XY has to be in run as administrator in order to do it. I just wanted to know if there was any way to do it without XY as admin.

Re: Create symlink without admin mode

Posted: 09 May 2025 11:01
by highend
Write out a .bat script (and run it afterwards) that self-elevates and executes the mklink command?

Code: Select all

    $batFile    = "%TEMP%\~xyplorer_mklink.bat";
    $batContent = <<<>>>
@ECHO OFF
REG QUERY "HKU\S-1-5-19" >NUL 2>NUL && GOTO :ENDUAC
SET "vbsFile=%TEMP%\~getAdmin.vbs"
IF "%1" NEQ "" (FOR /F "tokens=*" %%I IN ('ECHO %*') DO SET "param=%%~I")

ECHO Set UAC = CreateObject^("Shell.Application"^)> "%vbsFile%"
ECHO UAC.ShellExecute "cmd", "/c """"%~s0"" ""%param%""""", "", "runas", ^1>> "%vbsFile%"
CSCRIPT "%vbsFile%" //Nologo
IF EXIST "%vbsFile%" DEL /Q "%vbsFile%" >NUL 2>NUL
EXIT /B
:ENDUAC
    >>>;

    $batContent .= <crlf 2> . "your mklink stuff here...";

    writefile($batFile, $batContent, , "utf8");
    run $batFile, "%TEMP%", 0, 1;


Re: Create symlink without admin mode

Posted: 09 May 2025 18:04
by noembryo
kiwichick wrote: 09 May 2025 10:53
noembryo wrote: 08 May 2025 11:55 Without knowing any context on this question
I don't know what 'context' you want but XY scripting can create a symlink. However, XY has to be in run as administrator in order to do it. I just wanted to know if there was any way to do it without XY as admin.
This is exactly the context I needed.
Do you want to create a symlink, or you want to create a symlink by scripting?
My answer was for the former (it's the way I create symlinks in XYplorer without having administrator rights), but unfortunately you wanted an answer for the latter..

Re: Create symlink without admin mode

Posted: 10 May 2025 05:18
by kiwichick
noembryo wrote: 09 May 2025 18:04 This is exactly the context I needed.
Do you want to create a symlink, or you want to create a symlink by scripting?
My answer was for the former (it's the way I create symlinks in XYplorer without having administrator rights), but unfortunately you wanted an answer for the latter..
Yes it was for the latter. My apologies for not being clear about that.

Re: Create symlink without admin mode

Posted: 10 May 2025 05:19
by kiwichick
highend wrote: 09 May 2025 11:01 Write out a .bat script (and run it afterwards) that self-elevates and executes the mklink command?

Code: Select all

    $batFile    = "%TEMP%\~xyplorer_mklink.bat";
    $batContent = <<<>>>
@ECHO OFF
REG QUERY "HKU\S-1-5-19" >NUL 2>NUL && GOTO :ENDUAC
SET "vbsFile=%TEMP%\~getAdmin.vbs"
IF "%1" NEQ "" (FOR /F "tokens=*" %%I IN ('ECHO %*') DO SET "param=%%~I")

ECHO Set UAC = CreateObject^("Shell.Application"^)> "%vbsFile%"
ECHO UAC.ShellExecute "cmd", "/c """"%~s0"" ""%param%""""", "", "runas", ^1>> "%vbsFile%"
CSCRIPT "%vbsFile%" //Nologo
IF EXIST "%vbsFile%" DEL /Q "%vbsFile%" >NUL 2>NUL
EXIT /B
:ENDUAC
    >>>;

    $batContent .= <crlf 2> . "your mklink stuff here...";

    writefile($batFile, $batContent, , "utf8");
    run $batFile, "%TEMP%", 0, 1;

Thanks 😁👍

Re: Create symlink without admin mode

Posted: 20 May 2025 23:50
by WirlyWirly
highend wrote: 09 May 2025 11:01 Write out a .bat script (and run it afterwards) that self-elevates and executes the mklink command?
...
Thanks @highend, you saved me a lot of head scratching as I'm getting into symlinks :biggrin:

For anyone interested, I took highend's script and adjusted it slightly so that it will generate a symlink for every currently selected item. There's enough logic to differentiate between file/folder and format mklink appropriately.

Code: Select all

// Generate symlinks for each File/Folder currently selected.
// Elevates cmd to admin

$outputPrefix = 'link-';

// ================================ CODE ================================

    $batFile    = "<xydata>\Temp\symlinkGenerator.bat";

    $batContents = <<<STRING
@ECHO OFF
REG QUERY "HKU\S-1-5-19" >NUL 2>NUL && GOTO :ENDUAC
SET "vbsFile=<xydata>\Temp\~getAdmin.vbs"
IF "%1" NEQ "" (FOR /F "tokens=*" %%I IN ('ECHO %*') DO SET "param=%%~I")

ECHO Set UAC = CreateObject^("Shell.Application"^)> "%vbsFile%"
ECHO UAC.ShellExecute "cmd", "/c """"%~s0"" ""%param%""""", "", "runas", ^1>> "%vbsFile%"
CSCRIPT "%vbsFile%" //Nologo
IF EXIST "%vbsFile%" DEL /Q "%vbsFile%" >NUL 2>NUL
EXIT /B
:ENDUAC
STRING;

    foreach($item, <selitems :::>, ':::') {
        // Format 'mklink' depending on $item type (file|folder)
        $itemDirectory = getpathcomponent($item, 'path');
        $itemFilename = getpathcomponent($item, 'name');

        if (exists($item) == '1') {
            // $item is file
            $command = lax(mklink "$itemDirectory\$outputPrefix$itemFilename" "$item");
            $batContents .= <crlf> . $command;
        } elseif (exists($item) == '2') {
            // $item is folder
            $command = lax(mklink /D "$itemDirectory\$outputPrefix$itemFilename" "$item");
            $batContents .= <crlf> . $command;
        }
    }

    writefile($batFile, $batContents, , "utf8");
    run($batFile, , 0, 0);


Re: Create symlink without admin mode

Posted: 28 May 2025 04:18
by kiwichick
WirlyWirly wrote: 20 May 2025 23:50 I took highend's script and adjusted it slightly so that it will generate a symlink for every currently selected item. There's enough logic to differentiate between file/folder and format mklink appropriately.

Code: Select all

// Generate symlinks for each File/Folder currently selected.
// Elevates cmd to admin

$outputPrefix = 'link-';

// ================================ CODE ================================

    $batFile    = "<xydata>\Temp\symlinkGenerator.bat";

    $batContents = <<<STRING
@ECHO OFF
REG QUERY "HKU\S-1-5-19" >NUL 2>NUL && GOTO :ENDUAC
SET "vbsFile=<xydata>\Temp\~getAdmin.vbs"
IF "%1" NEQ "" (FOR /F "tokens=*" %%I IN ('ECHO %*') DO SET "param=%%~I")

ECHO Set UAC = CreateObject^("Shell.Application"^)> "%vbsFile%"
ECHO UAC.ShellExecute "cmd", "/c """"%~s0"" ""%param%""""", "", "runas", ^1>> "%vbsFile%"
CSCRIPT "%vbsFile%" //Nologo
IF EXIST "%vbsFile%" DEL /Q "%vbsFile%" >NUL 2>NUL
EXIT /B
:ENDUAC
STRING;

    foreach($item, <selitems :::>, ':::') {
        // Format 'mklink' depending on $item type (file|folder)
        $itemDirectory = getpathcomponent($item, 'path');
        $itemFilename = getpathcomponent($item, 'name');

        if (exists($item) == '1') {
            // $item is file
            $command = lax(mklink "$itemDirectory\$outputPrefix$itemFilename" "$item");
            $batContents .= <crlf> . $command;
        } elseif (exists($item) == '2') {
            // $item is folder
            $command = lax(mklink /D "$itemDirectory\$outputPrefix$itemFilename" "$item");
            $batContents .= <crlf> . $command;
        }
    }

    writefile($batFile, $batContents, , "utf8");
    run($batFile, , 0, 0);

Thanks for that but am I missing something? Do I need to do anything other than select a folder and run the script? I tried that and the symlink wasn't created. The bat file created by the script seems to have the correct information.

Re: Create symlink without admin mode

Posted: 28 May 2025 07:15
by jupe
If you wanted you could achieve this with a one liner, with no temp files. eg.

Code: Select all

e|open """<xy>"" /fresh /win=min /script=|::". report('new("link-{Name}", "symlink", "{Fullname}"); ', 1) ."exit n;| ""<curpath>""", e;
As the script is currently it is just for using in normal folders or non-recursive search results, you'd need to modify it if you wanted it to work in multi hierarchy search results.