2

我有一个批处理文件,通过将包含 .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”结束,而不是实际文件夹位置。我不知道如何解决这个问题。

4

3 回答 3

8

Here is a way to get the target using a little hybrid VBS/Batch file function.

@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
于 2013-10-29T12:17:40.780 回答
1

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

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

where<SUB>表示替代字符

于 2013-10-29T14:14:36.090 回答
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
于 2013-10-31T08:59:54.597 回答