vbs 脚本可更改在 7 中工作但在 XP 中不工作的快捷方式 [英] vbs script to change shortcuts working in 7 but not in XP

查看:8
本文介绍了vbs 脚本可更改在 7 中工作但在 XP 中不工作的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a script that will update desktop shortcuts in WinXP and Win7 (32 and 64bit). I'm having 2 problems, in XP, the target path of the shortcut won't change, and in both XP and 7, the "Start In" part of the shortcut won't change. Whats wrong here and how can I correct?

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 

推荐答案

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

Code was doing exactly what I told it to do, the default compare method used in vbs Replace function is Binary, which is case sensitive. For some reason paths in shortcuts are case insensitive. I added the options to do a vbTextCompare by adding > , 1, -1, 1 onto the end of my replace statements so they now look like this:

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

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

So its now replace Office14 with Office99 without regards to case. My target path in XP and the WorkingDirectory in 7 are now both changing!

这篇关于vbs 脚本可更改在 7 中工作但在 XP 中不工作的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆