从批处理脚本中的.lnk快捷方式获取源.exe文件名? [英] Getting the source .exe file name from .lnk shortcut in batch script?

查看:222
本文介绍了从批处理脚本中的.lnk快捷方式获取源.exe文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从.lnk快捷方式中批量提取源文件名.提取的文本必须采用(程序名称).(扩展名)形式.

I have to extract source filename from .lnk shortcut in batch. 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.

推荐答案

您可以使用wmic查询到

You can do it with a wmic query to win32_shortcutfile. Just make sure all your backslashes are backslash-escaped within %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
)

您想要的内容以%%J结尾.如果只需要目标filename.ext,请将其更改为%%~nxJ.如果只需要驱动器和路径,请将其更改为%%~dpJ.有关变量扩展的更多信息,请参见cmd控制台中help for的最后一页.

What you want ends up in %%J. If you only want the target filename.ext, change that to %%~nxJ. If you want only the drive and path, change it to %%~dpJ. See the last page of help for in a cmd console for more info about variable expansion.

这篇关于从批处理脚本中的.lnk快捷方式获取源.exe文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆