【问题标题】:windows ".lnk" shortcuts and batches don't mixWindows“.lnk”快捷方式和批处理不混合
【发布时间】:2013-11-08 08:11:54
【问题描述】:

我有一个批处理文件,通过将包含 .mp3s 的文件夹拖到批处理中来使用它。

@echo off
cd %~dp0
setlocal enabledelayedexpansion enableextensions
set FLDR="%1"
if not defined FLDR ( echo Drag a folder to the batch to play its contents.
pause
goto:EOF )
for %%x in (%FLDR%\*.mp3) do set "MP3=!MP3! "%%x""
mp3player %MP3%
pause

它适用于实际文件夹,但在拖动快捷方式时,变量 %FLDR% 以“c:\link location\folder.lnk”结束,而不是实际文件夹位置。 我不知道如何解决这个问题。

【问题讨论】:

  • lnk 文件无法批量访问。如果你启动一个文件夹链接,它会打开一个资源管理器窗口。

标签: batch-file syntax-error shortcut


【解决方案1】:

这是一种使用混合 VBS/Batch 文件功能获取目标的方法。

@echo off   
setlocal

Call :GetTarget "%~1" tgt 
echo %tgt%
pause
exit /b 


:GetTarget  
@echo off & setlocal
set gt=%temp%\_.vbs
echo set WshShell = WScript.CreateObject("WScript.Shell")>%gt%
echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>%gt%
echo wscript.Echo Lnk.TargetPath>>%gt%
set script=cscript //nologo %gt%
For /f "delims=" %%a in ( '%script% "%~1"' ) do set target=%%a
del %gt%
endlocal & set %~2=%target%
exit /b

【讨论】:

  • 想法是解决快捷方式,而不是创建! (我不会给你-1来保持你的满分!!!)
  • @LS_Dev 你真的尝试过代码吗?如果是这样,您会发现它不会创建任何东西。它只是使用 wscript.shell 对象的 createshortcut 方法来解析链接目标。
  • Ups...我一直在寻找打开链接的方法,但没有意识到CreateShortcut"Creates a new shortcut, or opens an existing shortcut"...我深表歉意!
【解决方案2】:

混合脚本!没有愚蠢的小临时文件。

::'<SUB>@echo off
::'<SUB>set shortcut=%~1
::'<SUB>if not defined shortcut goto 'usage
::'<SUB>if not %shortcut:~-4%==.lnk (if not %shortcut:~-4%==.url (set errorlevel=1
::'<SUB>goto 'usage ))
::'<SUB>if not exist %shortcut% (echo Error: Nonexistent shortcut
::'<SUB>set errorlevel=1
::'<SUB>goto:EOF )
::'<SUB>setlocal
::'<SUB>for /f "delims=" %%T in ('cscript //nologo //e:vbs %~nx0 "%shortcut%"') do set thing=%%T
::'<SUB>endlocal & set shortcut=%thing%
::'<SUB>goto:EOF
:'usage
::'<SUB>echo command-line shortcut redirect utility
::'<SUB>echo Usage: shortcut [file.lnk ^| file.url]
::'<SUB>echo The resulting link will be output to the %%shortcut%% variable.
::'<SUB>goto:EOF
set WshShell = WScript.CreateObject("WScript.Shell")
set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))
wscript.Echo Lnk.TargetPath

其中&lt;SUB&gt; 表示substitute character

【讨论】:

    【解决方案3】:

    更新:我在 Wayne's World of IT 找到了一个非常完整的 vbscript 解决方案,并对其进行了一些修改以满足我的需要:

    If WScript.Arguments.UnNamed.Count = 1 Then
     strShortcut = WScript.Arguments.UnNamed(0)
    Else
     WScript.Echo "Please supply the name of an lnk file or directory to read, eg c:\test.lnk or c:\shortcuts"
     WScript.Quit(1)
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(strShortCut) Then
     Set objFolder = objFSO.getFolder(strShortcut)
     For Each objfile in objFolder.Files
      If objfile.type = "Shortcut" Then
       Call Readshortcut(objFile.Path, strProperties)
       dtmCreationDate = objFile.DateCreated
       WScript.Echo dtmCreationDate & "," & strProperties
      End If
     Next
    ElseIf objFSO.FileExists(strShortCut) Then
     Call Readshortcut(strShortcut, strProperties)
     WScript.Echo strProperties
    Else
     WScript.Echo "Error: Could not read '" & strShortcut & "'"
     WScript.Quit(2)
    End If
    Set objFSO = Nothing
    Function Readshortcut(ByRef strShortcut, ByRef strProperties)
     set objWshShell = WScript.CreateObject("WScript.Shell")
     set objShellLink = objWshShell.CreateShortcut(strShortcut)
     strProperties = objShellLink.TargetPath & " " & objShellLink.Arguments
     Set objShellLink = Nothing
     Set objWshshell = Nothing
    End Function
    

    【讨论】:

      猜你喜欢
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2010-09-23
      • 1970-01-01
      相关资源
      最近更新 更多