windows symbolic link target

前端 未结 6 1056
死守一世寂寞
死守一世寂寞 2020-12-13 19:48

Say that I set up a symbolic link:

mklink  /D C:\\root\\Public\\mytextfile.txt C:\\root\\Public\\myothertextfile.txt

Editor\'s note: O

相关标签:
6条回答
  • 2020-12-13 20:30

    Maybe someone's interested in a C# method to resolve all directory symlinks in a directory similar to Directory.GetDirectories(). To list Junctions or File symlinks, simply change the regex.

    static IEnumerable<Symlink> GetAllSymLinks(string workingdir)
    {
        Process converter = new Process();
        converter.StartInfo = new ProcessStartInfo("cmd", "/c dir /Al") { RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = workingdir };
        string output = "";
        converter.OutputDataReceived += (sender, e) =>
        {
            output += e.Data + "\r\n";
        };
        converter.Start();
        converter.BeginOutputReadLine();
        converter.WaitForExit();
    
        Regex regex = new Regex(@"\n.*\<SYMLINKD\>\s(.*)\s\[(.*)\]\r");
    
        var matches = regex.Matches(output);
        foreach (Match match in matches)
        {
            var name = match.Groups[1].Value.Trim();
            var target = match.Groups[2].Value.Trim();
            Console.WriteLine("Symlink: " + name + " --> " + target);
    
            yield return new Symlink() { Name = name, Target = target };
        }
    }
    
    class Symlink
    {
        public string Name { get; set; }
        public string Target { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-13 20:32

    Write a batch file( get_target.bat) to get the target of the symbolic link:

    @echo off
    for /f "tokens=2 delims=[]" %%H in  ('dir /al %1 ^| findstr /i /c:"%2"') do (
        echo %%H
    )
    

    For example, to get the target of C:\root\Public\mytextfile.txt:

    get_target.bat C:\root\Public\ mytextfile.txt
    
    0 讨论(0)
  • 2020-12-13 20:34

    To complement Paul Verest's helpful answer:

    • While cmd's dir indeed shows the link type and target path, extracting that information is nontrivial - see below for a PowerShell alternative.

    • In order to discover all links in the current directory, use dir /aL.

    PowerShell (PSv5+) solutions:

    List all links and their targets in the current directory as full paths:

    PS> Get-ChildItem | ? Target | Select-Object FullName, Target
    
    FullName                      Target
    --------                      ------
    C:\root\public\mytextfile.txt {C:\root\Public\myothertextfile.txt}
    

    Determine a given link's target:

    PS> (Get-Item C:\root\Public\mytextfile.txt).Target
    C:\root\Public\myothertextfile.txt
    
    0 讨论(0)
  • 2020-12-13 20:38

    all credits to @SecurityAndPrivacyGuru , [cmd]

    complete batch script/function that reads symlink{|s in folder} and outputs list with them and their target paths

    @echo off
    setlocal enableExtensions enableDelayedExpansion
    cd /D "%~dp0"
    set br=^
    
    
    rem br;
    
    
    set "pafIf=<<pafToSymlink|pafToFolder>>"
    set "gIfZsymLink="
    for /f "tokens=*" %%q in ('dir "!pafIf!" /al /b') do (
        for /f "tokens=2 delims=[]" %%r in ('dir /al ^| findstr /i /c:"%%q"') do (
            set "gIfZsymLink=!gIfZsymLink!%%~fq>%%r!br!"
        )
    )
    set "gIfZsymLink=!gIfZsymLink:~0,-1!"
    rem echo "!gIfZsymLink!"
    
    for /f "tokens=1,2 delims=>" %%q in ("!gIfZsymLink!") do (
        echo symlink: %%q , filepath: %%r
    )
    
    
    :scIn
    rem endlocal
    pause
    rem exit /b
    
    0 讨论(0)
  • 2020-12-13 20:39

    Command Line options

    SuperUser Julian Knight's answer lists some resources to Windows equivalents for Linux commands. The most relevant command for this would be ln.exe. Here are links to download, and how to use these utilities.

    • http://unxutils.sourceforge.net/
    • http://schinagl.priv.at/nt/ln/ln.html
    • http://neosmart.net/Symlinks/

    Using Windows GUI

    Alternatively, if you want to see the target of symlinks using the Windows GUI interface (specifically, the file Properties window), install the Windows Link Shell Extension (direct link to the file download):

    SuperUser Julian Knight's answer worked well for me.

    With Windows Link Shell Extension installed, you can right-click on the link in Windows Explorer and check the properties. There is a tab that allows you to change the link directly.

    With Link Shell Extension installed:

    • When you right-click a file to open up "Properties" window, it includes an additional tab: "Link Properties", showing the Target of symlinks.
    • You can also edit this Target field to change the symlink's target.
    • And since the field is editable, copy-paste of the target link is easy.
    • As is scrolling to see the entire path of the target file!
      (In contrast, the "Location" field elsewhere in the Properties window does NOT allow scrolling to see the full path of the file you're examining.)

    BTW

    The WAMP server icon no longer appeared in the system tray (noticed) shortly after installing Link Shell Extension, which means that while WAMPP still runs (local websites work as expected), none of the WAMP functions could be accessed.
    It is not definitive that the install caused WAMPP tray icon to disappear, but the Good news is that after a reboot, the WAMPP re-appeared in the system tray, as normal. :-)

    0 讨论(0)
  • 2020-12-13 20:41

    As Harry Johnston said dir command shows the target of symbolic links

    2014/07/31  11:22    <DIR>          libs
    2014/08/01  13:53             4,997 mobile.iml
    2014/07/31  11:22               689 proguard-rules.pro
    2014/09/28  10:54    <JUNCTION>     res [\??\C:\Users\_____\mobile\src\main\res]
    
    0 讨论(0)
提交回复
热议问题