下面是一個VBScript,可以提取所有目標從位於您的桌面上的鏈接路徑的一個例子:
Option Explicit
Dim Ws,objStartFolder,objFSO,objFolder,colFiles
Dim objFile,strFilePath,Lnk
Set Ws = CreateObject("Wscript.Shell")
objStartFolder = Ws.SpecialFolders("Desktop")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFilePath = objFile.Path
If Ucase(objFSO.GetExtensionName(strFilePath)) = "LNK" Then
Call ExtractTargetPath(strFilePath)
End If
Next
'*************************************************************
Sub ExtractTargetPath(Lnk)
set Ws = CreateObject("WScript.Shell")
set Lnk = Ws.Createshortcut(Lnk)
WScript.echo "Link="& DblQuote(Lnk) & vbcrlf &_
"Target="& DblQuote(Lnk.TargetPath)
End Sub
'*************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*************************************************************
編輯:於19/06/2017 @ 14:00
您可以使用批處理文件使用該VBScript中也做到這一點:
Extract_Targets_Links.bat
@echo off
Title Extract targets paths from links
Set "LogFile=Links_Targets.txt"
If exist "%LogFile%" Del "%LogFile%"
Set "Desktop=%Userprofile%\Desktop"
Set "All_Users=%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup"
Set "Current_User=%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Set "VbsFile=%Tmp%\%~n0.vbs"
> "%VbsFile%" (
echo Option Explicit
echo Dim Ws,objStartFolder,objFSO,objFolder,colFiles
echo Dim objFile,strFilePath,Lnk
echo Set Ws = CreateObject("Wscript.Shell"^)
echo objStartFolder = WSH.Arguments(0^)
echo Set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo Set objFolder = objFSO.GetFolder(objStartFolder^)
echo Set colFiles = objFolder.Files
echo For Each objFile in colFiles
echo strFilePath = objFile.Path
echo If Ucase(objFSO.GetExtensionName(strFilePath^)^) = "LNK" Then
echo Call ExtractTargetPath(strFilePath^)
echo End If
echo Next
echo '*************************************************************
echo Sub ExtractTargetPath(Lnk^)
echo set Lnk = Ws.Createshortcut(Lnk^)
echo WScript.echo "Link="^& DblQuote(Lnk^) ^& vbcrlf ^&_
echo "Target="^& DblQuote(Lnk.TargetPath^) ^& vbcrlf ^&_
echo String(100,"*"^)
echo End Sub
echo '*************************************************************
echo Function DblQuote(Str^)
echo DblQuote = Chr(34^) ^& Str ^& Chr(34^)
echo End Function
echo '*************************************************************
)
For %%A in ("%Desktop%" "%All_Users%" "%Current_User%") Do (
Call :ExtractTarget "%%~A" "%LogFile%"
)
Start "" "%LogFile%" & Exit /b
::******************************************************************************
:ExtractTarget <Folder> <LogFile>
>> %2 (
echo =====================================================================
echo Links and their targets on %1
echo =====================================================================
)
For %%f in (%1) Do (
CScript /nologo "%VbsFile%" "%%~f" >> %2
)
Exit /b
::******************************************************************************
而你的問題是? (提示:「我需要一個循環」不是一個問題,這是一個編程任務。)你嘗試過什麼?你會得到什麼錯誤?你的推理是什麼? – Tomalak
如何使循環遍歷文件夾中的文件? – user3491432
這是一個非常基本的問題。你有沒有試過尋找答案? *「如何循環瀏覽VBS文件夾中的文件?」*對於搜索引擎來說似乎是一個很好的問題。 – Tomalak