Jump to content

Copying shortcuts to all users desktop with startup script


Recommended Posts

Posted

Hello.

Is it possible to copy shortcut from network share to all users desktop with computer startup script?

I have tried these: (Finnish version of Windows is in use so työpöytä = desktop)

copy /y \\server\share\shortcut.lnk "c:\documents and settings\all users\työpöytä\"

copy /y \\server\share\shortcut.lnk "c:\documents and settings\all users\työpöytä\shortcut.lnk"

..but it wont work. Is there a problem with ä and ö? Or is it even possible to do this with system account?

I would like to do this is because I want to upgrade one application that has been previously installed from

setup.exe and the new version of this app would be installed from .MSI. The problem is that users would have

shortcuts in their desktops pointing nowhere because the new version installs elsewhere than previous.


Posted

My suggestion is a VBScript which searches for that specific shortcut name on the all users desktop and replaces the location of the target path with your new one.

Posted

Thanks. It worked with VBScript. The code looked something like this:

Set oWS = WScript.CreateObject("WScript.Shell")

sLinkFile = "C:\documents and settings\all users\työpöytä\link.lnk"

Set oLink = oWS.CreateShortcut(sLinkFile)

oldpath=oLink.TargetPath

newpath="C:\program files\program\executable.exe"

oLink.WorkingDirectory="C:\program files\program"

oLink.TargetPath=newpath

oLink.Save

The original script failed to copy links to directory with letters ä and ö. Tested this with temp directory.

Posted
Thanks. It worked with VBScript. The code looked something like this:

Set oWS = WScript.CreateObject("WScript.Shell")

sLinkFile = "C:\documents and settings\all users\työpöytä\link.lnk"

Set oLink = oWS.CreateShortcut(sLinkFile)

oldpath=oLink.TargetPath

newpath="C:\program files\program\executable.exe"

oLink.WorkingDirectory="C:\program files\program"

oLink.TargetPath=newpath

oLink.Save

The original script failed to copy links to directory with letters ä and ö. Tested this with temp directory.

Try this

Const ALL_USERS_DESKTOP = &H19&

Dim Folder, FolderPath, newpath, oLink, oWS, Shell, sLinkFile

Set Shell = CreateObject("Shell.Application")
Set Folder = objShell.Namespace(ALL_USERS_DESKTOP)
Set FolderPath = objFolder.Self

Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = FolderPath.Path

Set oLink = oWS.CreateShortcut(sLinkFile)

oldpath=oLink.TargetPath
newpath="C:\program files\program\executable.exe"
oLink.WorkingDirectory="C:\program files\program"
oLink.TargetPath=newpath
oLink.Save

Posted

Well it seems from what you've posted that the task is a lot simpler than I'd presumed. I thought that the shortcut you were hoping to deploy was for an application on the server. To do what you'd originally hoped for something like this would probably suffice:

Const CommonDesktop = &H19&
Const OverwriteExisting = TRUE

'Shortcut
strSC = "\\Server\Share\link.lnk"

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(CommonDesktop)
Set objFolderItem = objFolder.Self

strAUDesktop = objFolderItem.Path

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strSC, strAuDesktop, OverWriteExisting

If you wanted to do as I originally intimated, i.e search all shortcuts on the all users desktop for a the old string and replace it with the new one, you could try something like this

Const CDtop = &H19&

Set oApp = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FilesystemObject")

' String in the target property to search for
sTargetStrOld = "C:\Program Files\OldProgDir\executable.exe"

' New string to set
sTargetStrNew = "C:\Program Files\NewProgDir\executable.exe"

Set oDir = oApp.Namespace(CDtop)
Set oDItem = oDir.Self
sLocn = oDItem.Path

ChkLnk(sLocn)

Sub ChkLnk (sFolder)
Set oFolder = oFso.GetFolder(sFolder)
Set oFiles = oFolder.Files

For Each oFile In oFiles
If LCase(oFso.GetExtensionName(oFile)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile)
If LCase(oLnk.TargetPath) = LCase(sTargetStrOld) Then
oLnk.TargetPath = sTargetStrNew
oLnk.Save
End If
End If
Next
End Sub

This one should work regardless of whether the shortcut name has been changed.

In both cases and the example provided by gunsmokingman, you'll note that we've used constants which specify the All Users Desktop folder regardless of its actual name/spelling etc.

Posted (edited)

Thanks again from answers.

Here is some backgroud.

The application is locally installed, and it is currently installed from setup.exe which install it to c:\program files\appdirectory\. It is also available in MSI package which installs it to c:\program files\appdirectory2\. I have a script that checks computers registry and if the string for MSI install is not found the uninstall is started and new version from MSI is installed. Something like this:

reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{the-string-goes-here}

if errorlevel==1 goto setup

if errorlevel==0 goto end

:setup

Uninstall script here

msiexec /i \\server\share\application.msi -qn

\\server\share\script.vbs

:end

exit

So this uninstalls previous version, installs new and modifies the shortcuts from common desktop (script.vbs). script.vbs will be done from those what you posted. This already does what I originally wanted it to do, but lets say that some computers are missing shortcuts from common desktop and users have created their own to their personal desktops. In that case I guess that I should also create logon script to check those. Would it work if I just replace

Const UDtop = &H10&

Set oApp = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FilesystemObject")

' String in the target property to search for
sTargetStrOld = "C:\Program Files\OldProgDir\executable.exe"

' New string to set
sTargetStrNew = "C:\Program Files\NewProgDir\executable.exe"

'Check for registry key
sTargetRegKey ="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{key-here}"

If RegKeyExists(sTargetRegKey) then

Set oDir = oApp.Namespace(UDtop)
Set oDItem = oDir.Self
sLocn = oDItem.Path

ChkLnk(sLocn)

Sub ChkLnk (sFolder)
Set oFolder = oFso.GetFolder(sFolder)
Set oFiles = oFolder.Files

For Each oFile In oFiles
If LCase(oFso.GetExtensionName(oFile)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile)
If LCase(oLnk.TargetPath) = LCase(sTargetStrOld) Then
oLnk.TargetPath = sTargetStrNew
oLnk.Save
End If
End If
Next
End Sub
Else
WScript.Quit

Function RegKeyExists(ByVal sRegKey)
' Returns True or False based on the existence of a registry key.
' This part is a compliment from Torgeir Bakken.

Dim sDescription

RegKeyExists = True
sRegKey = Trim (sRegKey)
If Not Right(sRegKey, 1) = "\" Then
sRegKey = sRegKey & "\"
End If

On Error Resume Next
oShell.RegRead "HKEYNotAKey\"
sDescription = Replace(Err.Description, "HKEYNotAKey\", "")

Err.Clear
oShell.RegRead sRegKey
RegKeyExists = sDescription <> Replace(Err.Description, sRegKey, "")
On Error Goto 0
End Function
End If

</Edit>

Edited by Yzöwl
Additional information appended
Posted

There shouldn't be a reason why not!

Const CDtop = &H19&
Const UDtop = &H10&

Set oApp = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FilesystemObject")

' String in the target property to search for
sTargetStrOld = "C:\Program Files\OldProgDir\executable.exe"

' New string to set
sTargetStrNew = "C:\Program Files\NewProgDir\executable.exe"

Set oDir = oApp.Namespace(CDtop)
Set oDItem = oDir.Self
sLocn1 = oDItem.Path

Set oDir = oApp.Namespace(UDtop)
Set oDItem = oDir.Self
sLocn2 = oDItem.Path

ChkLnk(sLocn1)
ChkLnk(sLocn2)

Sub ChkLnk (sFolder)
Set oFolder = oFso.GetFolder(sFolder)
Set oFiles = oFolder.Files

For Each oFile In oFiles
If LCase(oFso.GetExtensionName(oFile)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile)
If LCase(oLnk.TargetPath) = LCase(sTargetStrOld) Then
oLnk.TargetPath = sTargetStrNew
oLnk.Save
End If
End If
Next
End Sub

Posted

I use desktop redirection for that ... It can be a pain to make it work properly but when it does it's very good.

Posted
I use desktop redirection for that ... It can be a pain to make it work properly but when it does it's very good.
No you don't, we're simply fixing a broken shortcut the desktop locaton has no relevance.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...