2013-10-29 34 views
2

我有一个批处理文件,通过拖动包含.mp3s的文件夹到批处理中使用。窗口“.lnk”快捷方式和批次不混合

@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:\链接位置\ folder.lnk”结束了,而不是实际的文件夹位置。 我不知道如何解决这个问题。

+0

'lnk'文件不能由批量访问。如果您启动文件夹链接,则会打开资源管理器窗口。 – Endoro

回答

8

以下是使用小混合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 
+0

想法是解决捷径,而不是创建! (我不会给你-1保持你的完美分数!!!) –

+1

@LS_Dev你真的尝试过的代码?如果是这样,你会发现它没有创建任何东西。它只是使用wscript.shell对象的createshortcut方法来解析链接目标。 –

+0

Ups ...我一直在寻找一种方法来打开一个链接,并没有意识到'CreateShortcut' [“创建一个新的快捷方式,或打开一个现有的快捷方式”](http://msdn.microsoft.com/zh-cn/library/default.aspx)。 com/en-us/library/xsy6k3ys.aspx)...我的歉意! –

1

HYBRID SCRIPT!没有愚蠢的小临时文件。

::'<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 

<SUB>表示substitute character

0

更新:我在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 
相关问题