问题
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
回答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)
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!
来源:https://stackoverflow.com/questions/16323927/vbs-script-to-change-shortcuts-working-in-7-but-not-in-xp