windows windows符号链接目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10188485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:27:51  来源:igfitidea点击:

windows symbolic link target

windowssymlink

提问by Sree Ram

Say that I set up a symbolic link:

假设我设置了一个符号链接:

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

Editor's note: Option /D- which is for creating directorysymlinks - is at odds with targeting files, as in this example, which has caused some confusion. To create a filesymlink, simply omit /D.

编者注:Option /D- 用于创建目录符号链接 - 与定位文件不一致,如本例所示,这引起了一些混淆。要创建文件符号链接,只需省略/D.

Is there a way to see what the target of mytextfile.txtis, using the command line?

有没有办法mytextfile.txt使用命令行查看目标是什么?

采纳答案by Paul Verest

As Harry Johnston said dircommand shows the target of symbolic links

正如哈里约翰斯顿所说,dir命令显示符号链接的目标

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]

回答by mklement0

To complement Paul Verest's helpful answer:

为了补充Paul Verest 的有用回答

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

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

  • 虽然cmd'sdir确实显示了链接类型和目标路径,但提取该信息并不重要 - 请参阅下面的 PowerShell 替代方案。

  • 要发现当前目录中的所有链接,请使用dir /aL.

PowerShell(PSv5+) solutions:

PowerShell(PSv5+) 解决方案:

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

回答by hxysayhi

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

编写一个批处理文件(get_target.bat)来获取符号链接的目标:

@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:

例如,要获得目标C:\root\Public\mytextfile.txt

get_target.bat C:\root\Public\ mytextfile.txt

回答by JCH2k

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.

也许有人对解析类似于 Directory.GetDirectories() 的目录中的所有目录符号链接的 C# 方法感兴趣。要列出连接点或文件符号链接,只需更改正则表达式。

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; }
}

回答by ilias

all credits to @SecurityAndPrivacyGuru, [cmd]

@SecurityAndPrivacyGuru 的所有功劳,[cmd]

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

完整的批处理脚本/函数,读取符号链接{|文件夹中的 s} 并输出包含它们及其目标路径的列表

@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