使用vbs循环lnk文件并获取其目标路径

时间:2017-06-18 18:18:20

标签: for-loop vbscript

我想循环浏览特定文件夹中的一堆文件(有问题的文件是.lnk文件)。在那个循环中,我能够引用目标文件路径(所有文件夹)。

我正在这样做,所以我可以使用我发现的脚本来更改他们的图标,该图标是目标文件夹内的文件,所有文件都具有相同的名称cover.ico。我将用来更改图标的脚本是

Set sh = CreateObject("WScript.Shell")

lnkfile = "Path\To\LinkFile.lnk"

Set lnk = sh.CreateShortcut(lnkfile)

lnk.IconLocation = "Path\To\IconFile.ico"

lnk.Save

更改lnk图标的脚本工作我已经测试过它我只需要将它放入循环中。 我现在还已经知道如何使用此

获取.vbs当前目录的路径
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)

这样就可以自己处理.lnk文件路径,因为我将.vbs与.lnk文件在同一路径上运行。

1 个答案:

答案 0 :(得分:1)

以下是一个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
'*************************************************************

编辑:2017年6月19日@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
::******************************************************************************