回复 2# CrLf - @echo OFF
- call :main_20230214_010127 %*
- REM echo.&pause
- @goto :EOF
-
- @rem https://learn.microsoft.com/zh-cn/windows/win32/wmisdk/like-operator
-
- :main_20230214_010127
- @echo off
- set "filepath=%~f1"
- if not exist "%filepath%" set/p "filepath=filepath="
- REM 脱去环境变量 filepath 左右两侧的双引号。
- set filepath| findstr /i /r "^filepath=\".*\"$" > nul && set "filepath=%filepath:~1,-1%"
- if not exist "%filepath%" exit /b 1
- echo "lnkpath1=%filepath%"
- call :get_lnk_target3 filepath returnVal
- echo.
- echo.dir "%returnVal%"
- dir "%returnVal%"
- @goto :eof
-
- @rem Usage:
- rem 获取快捷方式的目标路径。
- :get_lnk_target3 {<@in &file=*.lnk> ,[@out target]}
- @echo off||code by tiandyoin&title ^Call :get_lnk_target4 {^<@in ^&file=*.lnk^> ,[@out target]}
- rem Windows 规定文件名违法字符: 【*\/|:"<>? 】
- rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.()(,& ;set ==!@#$%^&_+{}`-=[];',..txt
- rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.()(,& ;set ==!@#$%^&_+{}`-=[];',..txt.lnk
-
- call set "filepath=%%%~1%%"
- echo "lnkpath2=%filepath%"
- set "filepath=%filepath:\=\\%"
- echo "lnkpath3=%filepath%"
- set "filepath=%filepath:'=\'%"
- echo "lnkpath4=%filepath%"
- set "filepath=%filepath:[=[[]%"
- echo "lnkpath5=%filepath%"
- set "filepath=%filepath:_=[_]%"
- echo "lnkpath6=%filepath%"
- rem 似乎没法转义逗号??只能使用 Like 通配符 _(下划线)。
- setlocal enabledelayedexpansion
- set "filepath=!filepath:,=_!"
- echo "lnkpath7=!filepath!"
- set "filepath=!filepath:%%=[%%]!"
- endlocal & set "filepath=%filepath%"
- echo "lnkpath8=%filepath%"
-
- set target=
- for /f "skip=1 delims=" %%a in (
- 'wmic path win32_shortcutfile where "name like '%filepath%'" get target'
- ) do (
- set "target=%%~a"
- goto :BREAK
- )
- :BREAK
- set "target=%target:~0,-2%"
- if "%~2" neq "" (set "%~2=%target%") else echo "target=%target%"
- @goto :eof
复制代码 找遍全网也没有找到转义逗号的方法,只能使用 like+通配符的方法搜索。 |