获取文件关联的所有快捷方式.lnk

5 篇文章 0 订阅
@goto :main_20230214_010127

@rem Usage:
 rem "快捷方式的读取和运行.bat"  "C:\Google Chrome.lnk"
 
@rem "C:\Google Chrome.lnk" 右键“属性” - “目标” 修改为:
 rem "C:\Program Files\Google\Chrome\Application\chrome.exe" --enable-contextual-search --user-data-dir="D:\Program Files\Google\Chrome\User Data" --disk-cache-dir="D:\Program Files\Google\Chrome\cache"

:main_20230214_010127
	@echo off
	set my=123
	set "filepath=%~f1"
@	if defined filepath set filepath
	@rem 有特殊字符时,必须得使用 set/p 输入,要输入完整路径。
	if not exist "%filepath%" set/p "filepath=输入路径="
	set filepath| findstr /i /r "^filepath=\".*\"$" > nul && set "filepath=%filepath:~1,-1%"
@	call echo "filepath=%%filepath%%"
	if not exist "%filepath%" exit /b 1
@	echo "lnkpath1=%filepath%"
	set returnVal=
	call :get_lnk_target6 filepath returnVal
	echo "lnkpathX=%returnVal%"&echo.
	if defined returnVal (
@		echo.dir "%returnVal%"
@		dir "%returnVal%"
	)
@	pause
@goto :eof
 
:run_lnk
@echo off&setlocal EnableDelayedExpansion
set "p=%~1" 
if /i not "%~x1"==".LNK" (start "" /i "%~1") else (
	(echo Dim objShell, objFolder, objFolderItem, objShellLink, s
	echo Set objShell = CreateObject("Shell.Application"^)				''创建对象
	echo Set objFolder = objShell.NameSpace("%~dp1"^)					''LNK文件所在目录路径
	echo Set objFolderItem = objFolder.ParseName("%~nx1"^)				''LNK文件名
	echo Set objShellLink = objFolderItem.GetLink						''获取快捷方式对象
	echo Set wshShell=CreateObject("WScript.Shell"^)
	@REM 方法 8
	echo wshShell.Run """" ^& objShellLink.Path ^& """" ^& Chr(32^) ^& objShellLink.Arguments	''调用目标(程序或其它^)路径 ^+ 空格 ^+ 参数
	@REM 方法 7
	REM echo wshShell.Run """%~1"""		''直接调用LNK文件
	@REM 方法 6
	REM Replace(objShellLink.Arguments, " ", "%%20")
	REM chr(32); space(1); " "
	@REM 方法 5
	REM echo wshShell.Run Chr(34^) ^& objShellLink.Path ^& Chr(34^) ^& " " ^& Chr(34^) ^& objShellLink.Arguments ^& Chr(34^)
	@REM 方法 4
	REM echo Public Const vbQuote = """"
	REM echo WScript.Echo vbQuote '' 当vbQuote为四个双引号时,显示为一个;六个双引号显示为两个。
	REM echo wshShell.Run vbQuote ^& objShellLink.Path ^& vbQuote ^& " " ^& vbQuote ^& objShellLink.Arguments ^& vbQuote
	@REM 方法 3
	REM echo wshShell.Run """" ^& objShellLink.Path ^& """ """ ^& objShellLink.Arguments ^& """ "
	@REM 方法 2
	REM echo s = s ^& """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^)
	REM echo s = s ^& """--user-data-dir=""D:\Program""" ^& Chr(32^) ^& """Files\Google\Chrome\User""" ^& Chr(32^) ^& """Data"""
	REM echo wshShell.Run s
	@REM 方法 1
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe"""
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^) ^& "--enable-contextual-search --user-data-dir=""D:\Program Files\Google\Chrome\User Data"" --disk-cache-dir=""D:\Program Files\Google\Chrome\cache"""
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^) ^& "--user-data-dir=""D:\Program Files\Google\Chrome\User Data""" ^& Chr(32^) ^& "--disk-cache-dir=""D:\Program Files\Google\Chrome\cache"""
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^) ^& "--user-data-dir=""D:\Program Files\Google\Chrome\User Data"""
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^) ^& """""--user-data-dir=""D:\Program Files\Google\Chrome\User Data"""
	REM echo wshShell.Run """C:\Program Files\Google\Chrome\Application\chrome.exe""" ^& Chr(32^) ^& """--user-data-dir=""D:\Program""" ^& Chr(32^) ^& """Files\Google\Chrome\User""" ^& Chr(32^) ^& """Data"""
	@REM 参数 2
	REM echo s = s ^& "objShellLink.Path=" ^& objShellLink.Path ^& vbLf
	REM echo s = s ^& "objShellLink.Arguments=" ^& objShellLink.Arguments ^& vbLf
	REM echo WScript.Echo s
	Set wshShell = Nothing
	Set objShell = Nothing
	)>"%TEMP%\runlnk.vbs"
	"%TEMP%\runlnk.vbs"
	REM del /f /q "%TEMP%\runlnk.vbs"
)
goto :eof

:: ---------------------------------------------------------------------------------------------------------------------

@rem http://www.bathome.net/thread-42582-1-1.html#如何获取lnk快捷方式文件指向的目标程序的路径及其附带的参数

@echo off&setlocal EnableDelayedExpansion & 
set "p=%~1" & 
if /i not "%~x1"==".LNK" (
    echo exit
) else mshta VBScript:Execute("set wsh=CreateObject(""WScript.Shell""): set lnk=wsh.CreateShortcut(""%~1""): wsh.run ""!p!"" &"" """"""& lnk.TargetPath &"""""" """"""& lnk.Arguments &"""""" ""& ""/L"": close")


:: ---------------------------------------------------------------------------------------------------------------------

@REM LNK 快捷方式的属性主要包括以下几项:
 REM 1.目标(T)
 REM 2.起始位置(S)
 REM 3.快捷键(K)
 REM 4.运行方式(R)
 REM 5.备注(O)
 REM 6.快捷方式图标(C)
 REM 7.参数

@rem http://www.bathome.net/thread-33298-1-1.html#vbs获取并修改Lnk快捷方式的属性值
@rem http://www.netbox.cn/document/html/wsmthrun.htm#窗口风格_WindowStyle

@REM 创建快捷方式.vbs:

Dim WshShell, strDesk, strWinDir, objLink
Set WshShell = CreateObject("WScript.Shell")                    ''创建对象
strDesk = WshShell.SpecialFolders("DeskTop")                    ''桌面文件夹路径
strWinDir = WshShell.ExpandenVironmentStrings("%WinDir%")       ''系统文件夹路径
Set objLink = WshShell.CreateShortcut(strDesk & "\NotePad.LNK")
objLink.TargetPath = strWinDir & "\system32\notepad.exe"        ''目标路径
objLink.WorkingDirectory = strWinDir & "\system32"              ''工作目录(起始位置)
objLink.WindowStyle = 1                                         ''窗口风格(运行方式 1=常规,3=最大化,7=最小化)
objLink.Description = "记事本"                                  ''描述(备注)
objLink.IconLocation = strWinDir & "\system32\notepad.exe, 0"   ''设置图标
objLink.Save

:: ---------------------------------------------------------------------------------------------------------------------

@rem https://learn.microsoft.com/en-us/windows/win32/shell/shelllinkobject-object

@REM 获取快捷方式属性值.vbs:

Dim objShell, objFolder, objFolderItem, objShellLink, s
Set objShell = CreateObject("Shell.Application")                ''创建对象
Set objFolder = objShell.NameSpace(&H00)                        ''桌面文件夹路径
Set objFolderItem = objFolder.ParseName("NotePad.LNK")
Set objShellLink = objFolderItem.GetLink                        ''获取快捷方式对象
s = "目标路径:" & objShellLink.Path & vbLf
s = s & "工作目录:" & objShellLink.WorkingDirectory & vbLf
s = s & "快捷键:" & objShellLink.Hotkey & vbLf
s = s & "运行方式:" & objShellLink.ShowCommand & vbLf
s = s & "描述:" & objShellLink.Description & vbLf
s = s & "参数:" & objShellLink.Arguments & vbLf
WScript.Echo s

:: ---------------------------------------------------------------------------------------------------------------------

@REM 修改快捷方式属性值.vbs:

Dim objShell, objFolder, strWinDir, objFolderItem, objShellLink
Set objShell = CreateObject("Shell.Application")                ''创建对象
Set objFolder = objShell.NameSpace(&H00)                        ''桌面文件夹路径
strWinDir = objShell.NameSpace(&H24).Self.Path                  ''系统文件夹路径
Set objFolderItem = objFolder.ParseName("NotePad.LNK")
Set objShellLink = objFolderItem.GetLink                        ''获取快捷方式对象
objShellLink.Path = strWinDir & "\NotePad.exe" '                ''修改目标路径
objShellLink.WorkingDirectory = objFolder.Self.Path             ''修改工作目录为桌面
objShellLink.Hotkey = 1614                                      ''修改快捷键为 Ctrl + Alt + N
''Shift=256,Ctrl=512,Alt=1024,N(虚拟键位)=78,累加后值为1614
''虚拟键码表:http://msdn.microsoft.com/en-us/library/dd375731(VS.85).aspx
objShellLink.ShowCommand =  3                                   ''修改运行方式(1=常规 3=最大化 7=最小化)
objShellLink.Description = "这是一个快捷方式"                   ''修改描述
objShellLink.Arguments = WScript.ScriptFullName                 ''增加参数
objShellLink.SetIconLocation strWinDir & "\explorer.exe", 0     ''修改图标
objShellLink.Save                                               ''保存修改

:: ---------------------------------------------------------------------------------------------------------------------

https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-shortcutfile
https://www.coder.work/article/7768407#cmd - 如何在 WMIC 的 WHERE 子句中转义逗号和右括号?(同时存在)
https://stackoverflow.com/questions/37198153/how-to-escape-both-comma-and-closing-parenthesis-in-where-clause-of-wmic
https://cloud.tencent.com/developer/ask/sof/102394066#使用wmic时对字符串进行转义
https://learn.microsoft.com/zh-cn/windows/win32/wmisdk/where-clause
https://learn.microsoft.com/zh-cn/windows/win32/wmisdk/like-operator

@REM 由原文件获取快捷方式,要求快捷方式是有效的,当修改原文件路径时,可能导致快捷方式无效。
@REM 因为没有 "" 包含文件路径,这可能导致分隔符问题 (SPACE, TAB, ;, = 和 ,) 或特殊字符,如 ^, &、( 和 )。
@REM 【Like】 运算符使用下表中的通配符来匹配子字符串:
@REM 字符           说明
@REM %              任何包含 0(零)个或零个以上字符的字符串。 以下示例查找类名称中任何位置出现“Win”的所有实例:SELECT * FROM meta_class WHERE __Class LIKE "%Win%"
@               REM 查询字符串中使用的任何字面百分号'%'都必须放在 [](方括号)内进行转义。
@REM _(下划线)    任何单个字符。 查询字符串中使用的任何文本下划线'_'都必须放在 [](方括号)内进行转义。
@REM [ ]            指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。查询字符串中使用的任何字面方括号,只转义左方括号,即 [[]。
@REM ^              任何不在范围 ([^a-f]) 或集合 ([^abcdef]) 中的单个字符。


@rem Usage:
 rem		获取快捷方式的目标路径:
:get_lnk_target1 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target1 ^<@in "*.lnk"^> [@out target]
	rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	:: filename=filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt
	
	set "filepath=%~f1"
	
	REM wmic path win32_shortcutfile where "Name='%filepath:\=\\%'" get Target /value  2>nul
	setlocal enableDelayedexpansion 
		set "target="
		REM 以下有 Bug, 当快捷方式的路径中有逗号时,WMIC 会运行失败。
		for /f "skip=1 delims=" %%a in ('wmic path win32_shortcutfile where "Name='%filepath:\=\\%'" get Target 2^>nul') do (
			if not defined target (
				REM 以下有 Bug, for ... in (set) 中,逗号是分隔符,当目标路径中有逗号时,会被替换成空格。
				for %%b in (%%~a) do if not defined target (set "target=%%~b") else set "target=!target! %%~b"
			)
		)
	endlocal & if "%~2" neq "" (set "%~2=%target%") else echo "%target%"
@goto :eof


@rem Usage:
 rem		获取快捷方式的目标路径:
:get_lnk_target2 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target2 ^<@in "*.lnk"^> [@out target]
	rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	:: filename=filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt

	call set "filepath=%%%~1%%"

@	echo "lnkpath1=%filepath%"
	set  "filepath=%filepath:\=\\%"
@	echo "lnkpath2=%filepath%"
	set  "filepath=%filepath:'=\^'%"
	REM set  "filepath=%filepath:'=\'%"
@	echo "lnkpath3=%filepath%"
	@rem 无法解的问题:逗号和括号同时存在
	set  "filepath=%filepath:(=\(%"
	echo "lnkpath4=%filepath%"
	set  "filepath=%filepath:)=\)%"
	echo "lnkpath5=%filepath%"

	setlocal enabledelayedexpansion
		set "filepath=!filepath:%%=^%%!"
	endlocal & set "filepath=%filepath%"
@	echo "lnkpath6=%filepath%"

	for /f "delims=" %%i in ('"wmic path win32_shortcutfile where (name='%filepath%') get target"') do echo,%%i
@	goto :EOF

	setlocal enableDelayedexpansion 
		set "target="
		REM 以下有 Bug, 当快捷方式的路径中有逗号时,WMIC 会运行失败。
		for /f "skip=1 delims=" %%a in ('wmic path win32_shortcutfile where "Name='%filepath%'" get Target 2^>nul') do (
			if not defined target (
				set "target=%%~a"
				call :Trim target
			)
		)
	endlocal & if "%~2" neq "" (set "%~2=%target%") else echo "%target%"
@goto :eof


@rem Usage:
 rem		1.获取快捷方式的目标文件路径:
 rem		2.获取文件的所有快捷方式路径:
:get_lnk_target3 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target3 {^<@in ^&file=*.lnk^> ,[@out target]}
	rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	:: filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt

	call set "filepath=%%%~1%%"
@	echo "lnkpath2=%filepath%"
	set  "filepath=%filepath:\=\\%"
@	echo "lnkpath4=%filepath%"
	set  "filepath=%filepath:'=\'%"
@	echo "lnkpath5=%filepath%"
	REM set  "filepath=%filepath:`=``%"
	REM echo "lnkpath3=%filepath%"
	REM set  "filepath=%filepath:^=^^%"
	REM echo "lnkpath6=%filepath%"
	REM set  "filepath=%filepath:&=^&%"
	REM echo "lnkpath7=%filepath%"
	REM set  "filepath=%filepath:(=^(%"
	REM echo "lnkpath8=%filepath%"
	REM set  "filepath=%filepath:)=^)%"
	REM echo "lnkpath9=%filepath%"
	REM set  "filepath=%filepath:;=^;%"
	REM echo "lnkpathA=%filepath%"

	set  "filepath=%filepath:[=[[]%"
@	echo "lnkpath9=%filepath%"
	set  "filepath=%filepath:_=[_]%"
@	echo "lnkpathA=%filepath%"

	REM :: 似乎没法转义逗号??只能使用 Like + 通配符 _(下划线)。 
	setlocal enabledelayedexpansion
		set "filepath=!filepath:,=_!"
@		echo "lnkpathB=!filepath!"
		set "filepath=!filepath:%%=[%%]!"
	endlocal & set "filepath=%filepath%"
@	echo "lnkpathC=%filepath%"

	if /i "%filepath:~-4%"==".lnk" (
		@REM 由[快捷方式]获取[原文件]路径
		wmic path win32_shortcutfile where "Name like '%filepath%'" get Target
	) else (
		@REM 由[原文件]获取[所有快捷方式]
		wmic path win32_shortcutfile where "Target like '%filepath%'" get Name
	)
@goto :EOF

	set target=
	if /i "%filepath:~-4%"==".lnk" (
		@REM 由[快捷方式]获取[原文件]路径
		for /f "skip=1 delims=" %%a in (
			'wmic path win32_shortcutfile where "Name like '%filepath%'" get Target'
		) do (
			set "target=%%~a"
			@goto :BREAK
		)
	) else (
		@REM 由[原文件]获取[所有快捷方式]
		for /f "skip=1 delims=" %%a in (
			'wmic path win32_shortcutfile where "Target like '%filepath%'" get Name'
		) do (
			set "target=%%~a"
			@goto :BREAK
		)
	)

	:BREAK
	set "target=%target:~0,-3%"
	if "%~2" neq "" (set "%~2=%target%") else echo "target=%target%"
@goto :eof


@rem Usage:
 rem		1.获取快捷方式的目标文件路径:
 rem		2.获取文件的所有快捷方式路径:
:get_lnk_target4 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target4 {^<@in ^&file=*.lnk^> ,[@out target]}
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	rem filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt

	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%"
	REM set  "filepath=%filepath:(=^(%"
	REM echo "lnkpath8=%filepath%"
	REM set  "filepath=%filepath:)=^)%"
	REM echo "lnkpath9=%filepath%"

::	if /i "%filepath:~-4%"==".lnk" (
::		@REM 由[快捷方式]获取[原文件]路径
::		powershell -Command Get-WmiObject -Query """Select Target from Win32_ShortcutFile where Name='%filepath%' """ ^| select -ExpandProperty Target
::	) else (
::		@REM 由[原文件]获取[所有快捷方式]
::		powershell -Command Get-WmiObject -Query """Select Name from Win32_ShortcutFile where Target='%filepath%' """ ^| select -ExpandProperty Name
::	)
::@	goto :EOF
	rem 想不出阻止变量求值的通用方法
	setlocal enabledelayedexpansion
		set "filepath=!filepath:%%temp%%=%%"temp"%%!"
		set "filepath=!filepath:%%my%%=%%"my"%%!"
		REM set "filepath=!filepath:%%=""%%""!"
	endlocal & set "filepath=%filepath%"
	echo "lnkpath6=%filepath%"

	set target=
	if /i "%filepath:~-4%"==".lnk" (
		@REM 由[快捷方式]获取[原文件]路径
		for /f "delims=" %%a in (
			'powershell -Command Get-WmiObject -Query """Select Target from Win32_ShortcutFile where Name='%filepath%' """ ^^^| select -ExpandProperty Target '
		) do (
			set "target=%%~a"
			@goto :BREAK
		)
	) else (
		@REM 由[原文件]获取[所有快捷方式]
		for /f "delims=" %%a in (
			'powershell -Command Get-WmiObject -Query """Select Name from Win32_ShortcutFile where Target='%filepath%' """ ^^^| select -ExpandProperty Name '
		) do (
			set "target=%%~a"
			@goto :BREAK
		)
	)
	:BREAK
	if "%~2" neq "" (set "%~2=%target%") else echo "target=%target%"
@	goto :eof

	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%"
	set  "filepath=%filepath:&=^&%"
@	echo "lnkpath7=%filepath%"
	REM powershell -Command "Get-WmiObject -Query """Select Target from Win32_ShortcutFile where Name='%filepath%' """ | select -ExpandProperty Target"

	set  "filepath=%filepath:(=^(%"
@	echo "lnkpath8=%filepath%"
	set  "filepath=%filepath:)=^)%"
@	echo "lnkpath9=%filepath%"
	set  "filepath=%filepath:;=^;%"
@	echo "lnkpathA=%filepath%"
	set target=
	for /f "delims=" %%a in (
		'powershell -Command "Get-WmiObject -Query """Select Target from Win32_ShortcutFile where Name='%filepath%' """ | select -ExpandProperty Target" '
	) do (
		set "target=%%~a"
		@goto :BREAK
	)
	:BREAK
@		set target
	if "%~2" neq "" (set "%~2=%target%") else echo "target=%target%"
@goto :eof

:: ---------------------------------------------------------------------------------------------------------------------

http://www.bathome.net/viewthread.php?tid=3418&page=1&extra=#pid274258#批处理获取快捷方式指向的路径

@rem Usage:
 rem		获取快捷方式的目标路径:
 rem		直接读取 lnk 的内容,取出第二串 ASCII 路径。
:get_lnk_target5 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target5 ^<@in "*.lnk"^> [@out target]
	rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	:: filename=;%1,%~2%,%~,!%~3!,!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set ==!@#$%^&_+{}`-`=[];'',..txt
	:: filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt
	call set "filepath=%%%~1%%"
	
@	echo "lnkpath2=%filepath%"

	setlocal enabledelayedexpansion
		set "filepath=!filepath:%%=""%%""!"
	endlocal & set "filepath=%filepath%"
@	echo "lnkpath3=%filepath%"

	for /f "skip=2 delims=" %%a in ('find ":\" "%filepath%"') do (
		if "%~2" neq "" (set "%~2=%%a") else echo "target=%%a"
@		goto :eof
	)
@goto :eof


@rem Usage:
 rem		获取快捷方式的目标路径:
:get_lnk_target6 {<@in &file=*.lnk> ,[@out target]}
	@echo off||code by tiandyoin&title ^Call :get_lnk_target6 ^<@in "*.lnk"^> [@out target]
	rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
	rem Windows 规定文件名违法字符:      【*\/|:"<>?	】
	:: filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt

	call set "filepath=%%%~1%%"
	set "$=%TEMP%\$.vbs"

	( echo Dim WSH, objShellLink
	  echo Set WSH = CreateObject("WScript.Shell"^)
	  echo Set objShellLink = WSH.CreateShortcut("%filepath%"^)
	  echo WScript.Echo """" ^& objShellLink.TargetPath ^& """" 
	) > "%$%"

	for /f "delims=" %%i in ('CScript //nologo "%$%"') do (
		if "%~2" neq "" (set "%~2=%%~i") else echo "target=%%~i"
		@goto :eof
	)
@	goto :eof

	( echo Dim WSH, objShellLink
	  echo Set WSH = CreateObject("WScript.Shell"^)
	  echo Set objShellLink = WSH.CreateShortcut(WScript.Arguments(0^)^)
	  REM echo s = "目标路径:"     ^& """" ^& objShellLink.TargetPath        ^& """" ^& vbLf
	  REM echo s = s ^& "工作目录:" ^& """" ^& objShellLink.WorkingDirectory ^& """" ^& vbLf
	  REM echo s = s ^& "快捷键:"   ^& """" ^& objShellLink.Hotkey           ^& """" ^& vbLf
	  REM echo s = s ^& "运行方式:" ^& """" ^& objShellLink.WindowStyle      ^& """" ^& vbLf
	  REM echo s = s ^& "图标:"     ^& """" ^& objShellLink.IconLocation     ^& """" ^& vbLf
	  REM echo s = s ^& "描述:"     ^& """" ^& objShellLink.Description      ^& """" ^& vbLf
	  REM echo WScript.Echo s
	  echo WScript.Echo """" ^& objShellLink.TargetPath ^& """" 
	) > "%$%"
	
	setlocal enabledelayedexpansion
		set "filepath=!filepath:%%="%%"!"
	endlocal & set "filepath=%filepath%"
@	echo "lnkpath2=%filepath%"

	for /f "delims=" %%i in ('CScript //E:VBScript //nologo "%$%" "%filepath%"') do (
		if "%~2" neq "" (set "%~2=%%~i") else echo "target=%%~i"
		@goto :eof
	)
@goto :eof


:: ---------------------------------------------------------------------------------------------------------------------

@rem Usage:
 rem		需要调用者提供“延迟变量扩展环境”。  
:Trim  <RefOfStrLine [,L[eft] ,R[ight]]>
	@echo off&setlocal enabledelayedexpansion
		@rem  13    0x0d           <Carriage Return>    回车符    调用方式: !$0d!
		for /f %%a in ('copy /z "C:\Windows\System32\drivers\etc\hosts" nul') do set $0d=%%a
		set fst=0
		set lst=0
		set "input=!%~1!"
		@if not defined input goto :EOF
		@if /i not "%~2"=="L" if /i not "%~2"=="Left" if /i not "%~2"=="" goto :Trim.lTrim.EOF
		:Trim.lTrim
			set ch=!input:~0,1!
			@rem 如果 ch 是双引号,需要凑成对组成引用闭环。 
			@if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="		" if not "!ch!!ch!"=="!$0d!!$0d!" goto :Trim.lTrim.EOF
			set "input=!input:~1!"
			set /a fst+=1
		@goto :Trim.lTrim
		:Trim.lTrim.EOF
		if /i not "%~2"=="R" if /i not "%~2"=="Right" if /i not "%~2"=="" if /i not "%~3"=="R" if /i not "%~3"=="Right" if /i not "%~3"=="" goto :Trim.EOF
		:Trim.rTrim
			set ch=!input:~-1!
			@if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="		" if not "!ch!!ch!"=="!$0d!!$0d!" goto :Trim.EOF
			set "input=!input:~0,-1!"
			set /a lst-=1
		@goto :Trim.rTrim
		:Trim.EOF
		if !lst!==0 (set "lst=") else (set "lst=,!lst!")
	endlocal & set "%~1=!%~1:~%fst%%lst%!"
@goto :EOF

:: ---------------------------------------------------------------------------------------------------------------------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值