vbs脚本可以更改在7中工作但不在XP中的快捷方式

时间:2013-05-01 18:18:20

标签: shell vbscript windows-xp shortcut

我正在尝试编写一个脚本,用于更新WinXP和Win7(32和64位)中的桌面快捷方式。我有2个问题,在XP中,快捷方式的目标路径不会改变,而在XP和7中,快捷方式的“开始”部分不会改变。这里有什么问题,我该如何纠正?

If InStr(GetWindowsVer(), "XP") > 0 then
    IterateFolder("C:\Documents and Settings\")
Elseif InStr(GetWindowsVer(), "7") > 0 then
    IterateFolder("C:\Users\")
End if

Sub IterateFolder(folderPath)
    Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput, subFolder
    strFolderToSearch = folderPath

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
    Set colSubfolders = objRootFolder.SubFolders

    For Each objFolder in colSubfolders

            subFolder = objFolder.Path & "\Desktop"

            Set objFSO1 = CreateObject("Scripting.FileSystemObject")
            Set objFolder1 = objFSO.GetFolder(subFolder)

            Set colFiles = objFolder1.Files
            For Each objFile in colFiles
            If strcomp(right(objFile.name,4),".lnk",vbTexctCompare) = 0 then                

                Set Shell = CreateObject("WScript.Shell")
                Set Link = Shell.CreateShortcut(objFile.Path)

                if instr(Link.TargetPath, "Office") > 0 then
                    strOutput = objFile.Path
                    strOutput = strOutput & vbCr & vbLf & Link.TargetPath
                    strOutput = strOutput & vbCr & vbLf & Link.WorkingDirectory
                    MsgBox strOutput     'This line returns expected data'

                    Link.TargetPath = Replace(Link.TargetPath, "Office11", "Office14"

                    if GetBits = "64" Then
                        Link.TargetPath = Replace(Link.TargetPath, "Program Files\", "Program Files (x86)\")
                    End if

                    Link.WorkingDirectory = Replace(Link.WorkingDirectory, "Office11", "Office14")
                    Link.Save

                End if
            End if
        Next
    Next
End Sub 

1 个答案:

答案 0 :(得分:0)

Code完全按照我的要求去做,vbs中使用的默认比较方法Replace函数是Binary,它区分大小写。由于某种原因,快捷方式中的路径不区分大小写。我添加了选项以通过在我的替换语句的末尾添加> , 1, -1, 1来执行vbTextCompare,所以它们现在看起来像这样:

Link.TargetPath = Replace(Link.TargetPath, "Office14", "Office99", 1, -1, 1)

所以它现在用Office99替换Office14而不考虑案例。我在XP中的目标路径和7中的WorkingDirectory现在都在改变!