Get Target of Shortcut Folder

Get target of shortcut folder

I think you will need to use COM and add a reference to "Microsoft Shell Control And Automation", as described in this blog post:

Here's an example using the code provided there:

namespace Shortcut
{
using System;
using System.Diagnostics;
using System.IO;
using Shell32;

class Program
{
public static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}

return string.Empty;
}

static void Main(string[] args)
{
const string path = @"C:\link to foobar.lnk";
Console.WriteLine(GetShortcutTargetFile(path));
}
}
}

Get target of shortcut (.lnk) file with powershell

You have made an error in the property; as wOxxOm suggests, you should be using TargetPath rather than Target:

$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('<full-path-to-shortcut>').TargetPath

Google and MSDN were indeed helpful here; additionally, piping objects to Get-Member can often be useful and educational. This question also shows how to manipulate shortcuts using PowerShell, and uses the same technique as seen here.

If you want the arguments to the executable as well, those are stored separately:

$arguments = $sh.CreateShortcut('<full-path-to-shortcut>').Arguments

Again, piping objects to Get-Member - in this case, the object returned by WScript.Shell.CreateShortcut() - provides useful information.

Extract Path from shortcut link - windows batch

You can do it with a wmic query to win32_shortcutfile. Just make sure all your backslashes are backslash-escaped within %filename%.

Syntax:

batfile shortcutfile.lnk

Code:

@echo off
setlocal

rem // ensure user supplied a filename with a .lnk extension
if /i "%~x1" neq ".lnk" (
echo usage: %~nx0 shortcut.lnk
goto :EOF
)

rem // set filename to the fully qualified path + filename
set "filename=%~f1"

rem // get target
for /f "delims=" %%I in (
'wmic path win32_shortcutfile where "name='%filename:\=\\%'" get target /value'
) do for /f "delims=" %%# in ("%%~I") do set "%%~#"

rem // preserve ampersands
setlocal enabledelayedexpansion
echo(!target!

Reading the target of a .lnk file in Python?

Create a shortcut using Python (via WSH)

import sys
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\\test.lnk")
shortcut.Targetpath = "t:\\ftemp"
shortcut.save()


Read the Target of a Shortcut using Python (via WSH)

import sys
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\\test.lnk")
print(shortcut.Targetpath)


Related Topics



Leave a reply



Submit