Reputation: 859
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?
Upvotes: 40
Views: 48292
Reputation: 174
Addendum to accepted answer, viewing symlinks in Windows GUI:
Each of the files and folders below contain the same 2KB data.
Upvotes: 0
Reputation: 17960
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.
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:
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. :-)
Upvotes: 0
Reputation: 1987
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
Upvotes: 5
Reputation: 334
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
Upvotes: 0
Reputation: 439822
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
Upvotes: 22
Reputation: 3621
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; }
}
Upvotes: 1
Reputation: 64012
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]
Upvotes: 30