简体   繁体   English

从批处理脚本中的.lnk快捷方式获取源.exe文件名?

[英]Getting the source .exe file name from .lnk shortcut in batch script?

I have to extract source filename from .lnk shortcut in batch. 我必须从.lnk快捷方式中批量提取源文件名。 The extracted text must be in (program name).(extension) form. 提取的文本必须采用(程序名称)。(扩展名)形式。

I must admit that I'm a complete laic, when it comes to batch (or any scripting actually), so any help concerning my question is highly appreciated. 我必须承认,对于批处理(或实际上是任何脚本编写),我是一个完全的老手,因此,非常感谢与我的问题有关的任何帮助。

You can do it with a wmic query to win32_shortcutfile . 您可以通过wmic查询win32_shortcutfile实现 Just make sure all your backslashes are backslash-escaped within %filename% . 只要确保在%filename%中将所有反斜杠转义即可。

@echo off
setlocal

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

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

:: convert single backslashes to double
set "filename=%filename:\=\\%"

:: get target
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
    echo(%%J
)

What you want ends up in %%J . 您想要的结果以%%J结尾。 If you only want the target filename.ext , change that to %%~nxJ . 如果只需要目标filename.ext%%~nxJ更改为%%~nxJ If you want only the drive and path, change it to %%~dpJ . 如果只需要驱动器和路径,请将其更改为%%~dpJ See the last page of help for in a cmd console for more info about variable expansion. 有关变量扩展的更多信息,请参见cmd控制台中help for的最后一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM