You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows tree命令如何显示.lnk快捷方式的目标路径?

Windows下显示目录树时展示.lnk快捷方式目标的方法

Windows自带的tree命令没有类似Linux tree -l的参数来解析并显示.lnk快捷方式的目标路径,以下是几种可行的解决办法:

方法一:PowerShell自定义脚本实现

用PowerShell编写脚本,遍历目录树的同时读取.lnk文件的目标路径,输出带链接信息的目录结构:

function Show-TreeWithLinks {
    param(
        [string]$Path = "."
    )
    # 获取当前路径的根目录深度基准
    $rootDepth = ($Path -split "\\").Count
    Get-ChildItem -Path $Path -Recurse | Sort-Object { $_.FullName.Length } | ForEach-Object {
        # 计算当前项的缩进层级
        $currentDepth = ($_.FullName -split "\\").Count - $rootDepth
        $indent = "    " * ($currentDepth - 1)
        
        if ($_.PSIsContainer) {
            Write-Host "$indent└───$($_.Name)"
        } else {
            if ($_.Extension -eq ".lnk") {
                # 读取快捷方式目标
                $shell = New-Object -ComObject WScript.Shell
                $shortcut = $shell.CreateShortcut($_.FullName)
                $target = $shortcut.TargetPath
                # 简化目标路径(如果是当前目录下的文件,显示相对路径)
                if ($target.StartsWith($Path)) {
                    $target = $target.Substring($Path.Length + 1)
                }
                Write-Host "$indent│   $($_.Name) -> $target"
            } else {
                Write-Host "$indent│   $($_.Name)"
            }
        }
    }
}

# 调用脚本,默认当前目录,可指定路径如 Show-TreeWithLinks -Path "C:\your\path"
Show-TreeWithLinks

使用说明:

  1. 将上述代码保存为TreeWithLinks.ps1文件
  2. 打开PowerShell,执行Set-ExecutionPolicy RemoteSigned -Scope CurrentUser(临时允许执行本地脚本,执行后可改回Restricted)
  3. 运行脚本:.\TreeWithLinks.ps1,或指定目标路径运行

方法二:使用第三方工具

1. GNUWin32 Tree工具

GNUWin32提供的tree工具支持类似Linux的-l参数,可解析Windows快捷方式并显示目标路径:

  • 下载安装GNUWin32工具集后,在命令行执行:tree -l C:\your\path
  • 输出格式与Linux tree -l类似,会自动标注.lnk的目标路径

2. lsd(现代文件列表工具)

lsd是一款开源的现代文件列表工具,支持树状显示并自动解析.lnk快捷方式:

  • 通过包管理器安装(如Scoop:scoop install lsd,或Chocolatey:choco install lsd)
  • 执行命令:lsd --tree C:\your\path,输出会直接显示.lnk文件的目标路径

方法三:批处理+VBScript批量生成报告

如果需要生成文本格式的目录树报告,可结合批处理和VBScript读取.lnk目标:

步骤1:创建VBS脚本GetLnkTarget.vbs

Set objArgs = WScript.Arguments
If objArgs.Count = 0 Then WScript.Quit
Set objShell = CreateObject("WScript.Shell")
Set objShortcut = objShell.CreateShortcut(objArgs(0))
WScript.Echo objShortcut.TargetPath
@echo off
setlocal enabledelayedexpansion
set "ROOT=%cd%"
:: 生成基础目录树结构到临时文件
tree /f > temp_tree.txt
:: 替换所有.lnk行,追加目标路径
for /f "tokens=*" %%i in (temp_tree.txt) do (
    set "line=%%i"
    echo !line! | findstr /i "\.lnk$" >nul
    if not errorlevel 1 (
        for /f "delims=" %%j in ('cscript //nologo GetLnkTarget.vbs "!ROOT!\!line:*   =!"') do (
            echo !line! -> %%j
        )
    ) else (
        echo !line!
    )
)
del temp_tree.txt

使用说明:

将两个文件放在同一目录,运行TreeWithLinks.bat,会在命令行输出带链接目标的目录树。


内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:45:24