问题
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 directory symlinks - is at odds with targeting files, as in this example, which has caused some confusion. To create a file symlink, simply omit /D
.
Is there a way to see what the target of mytextfile.txt
is, using the command line?
回答1:
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]
回答2:
To complement Paul Verest's helpful answer:
While
cmd
'sdir
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
回答3:
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
回答4:
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; }
}
回答5:
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
来源:https://stackoverflow.com/questions/10188485/windows-symbolic-link-target