Solved

Restructure VB Script

Posted on 2014-01-14
5
226 Views
Last Modified: 2014-01-16
I have a simple script (see attached) that I'd like to restructure and clean up and need some analysis.

In the script the first piece create's a shortcut but then the shortcut is deleted in the second part.  The second part is still needed because I want to effectively update the shortcut because of a targetpath change.  This script works fine if the new shortcut name isn't the same as the deleted ones. So it seems I need the deletion piece before the create new but I'm not sure how to do that.

Also, I'd like to know if there is any thing extraneous that can be removed.  For instance in the second part I still have to Set Shell when It seems like I already did that at the top.
myscript.txt
0
Comment
Question by:Vontech615
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 39781270
Hi, one other method you might like to use to do this, is before adding any new shortcuts, go through each of the .lnk files that exist on the desktop, and if they contain the old path, you can delete the file.  After that, just create your new one and you would know that you always have only one, which is new.

I think this should work, but I haven't tested it.

Regards,

Rob.

L_Welcome_MsgBox_Message_Text = "A shortcut to the AR File Checks folder" & vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text ="Windows Scripting Host Sample"
 
Call Welcome()

Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")

Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = WSHShell.SpecialFolders("Desktop")

' Specify a part of the old path here. If it is found in a shortcut's target path, it will be deleted
strOldPath = "C:\OldPath\"
' Delete any lnk file on the desktop that contains the old path
For Each objFile In FSO.GetFolder(DesktopPath)
	If Right(LCase(objFile.Name), 4) = ".lnk" Then
		Set objShortcut = WSHShell.CreateShortcut(objFile.Path)
		If InStr(1, objShortcut.TargetPath, strOldPath, vbTextCompare) > 0 Then
			blnDeleteFile = True
		Else
			blnDeleteFile = False
		End If
		Set objShortcut = Nothing
		If blnDeleteFile = True Then FSO.DeleteFile objFile.Path, True
	End If
Next

' Create the new shortcut
Dim MyShortcut, MyDesktop, DesktopPath
' Read desktop path using WshSpecialFolders object
DesktopPath =WSHShell.SpecialFolders("Desktop")
' Create a shortcut object on the desktop
Set MyShortcut =WSHShell.CreateShortcut(DesktopPath & "\AR File Checks.lnk")
' Set shortcut object properties and save it
MyShortcut.TargetPath = "C:\Local Cloud\Shared\Sites\Cambridge\Compliance\AR File Checks"
MyShortcut.save
	
WScript.Echo "A shorcut to the AR File Checks Folder as been successfully created on your desktop.  Any old shortcuts have been deleted.  Please close all programs and restart your computer."
 
Sub Welcome()
	Dim intDoIt
	intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text )
	If intDoIt = vbCancel Then
		WScript.Quit
	End if
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Vontech615
ID: 39782220
Ok I'm getting "Object doesn't support this property or method" on Line 15.
Error code: 800A01B6

L_Welcome_MsgBox_Message_Text = "A shortcut to the AR File Checks folder" & vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text ="Windows Scripting Host Sample"
 
Call Welcome()

Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")

Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = WSHShell.SpecialFolders("Desktop")

' Specify a part of the old path here. If it is found in a shortcut's target path, it will be deleted
strOldPath = "C:\Local Cloud\Shared\Sites\Cambridge\AR File Checks"
' Delete any lnk file on the desktop that contains the old path
For Each objFile In FSO.GetFolder(DesktopPath)
	If Right(LCase(objFile.Name), 4) = ".lnk" Then
		Set objShortcut = WSHShell.CreateShortcut(objFile.Path)
		If InStr(1, objShortcut.TargetPath, strOldPath, vbTextCompare) > 0 Then
			blnDeleteFile = True
		Else
			blnDeleteFile = False
		End If
		Set objShortcut = Nothing
		If blnDeleteFile = True Then FSO.DeleteFile objFile.Path, True
	End If
Next

' Create the new shortcut
Dim MyShortcut, MyDesktop, DesktopPath
' Read desktop path using WshSpecialFolders object
DesktopPath =WSHShell.SpecialFolders("Desktop")
' Create a shortcut object on the desktop
Set MyShortcut =WSHShell.CreateShortcut(DesktopPath & "\AR File Checks.lnk")
' Set shortcut object properties and save it
MyShortcut.TargetPath = "C:\Local Cloud\Shared\Sites\Cambridge\Compliance\AR File Checks"
MyShortcut.save
	
WScript.Echo "A shorcut to the AR File Checks Folder as been successfully created on your desktop.  Any old shortcuts have been deleted.  Please close all programs and restart your computer."
 
Sub Welcome()
	Dim intDoIt
	intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text )
	If intDoIt = vbCancel Then
		WScript.Quit
	End if
End Sub

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 39784064
D'oh!  Change thie line
For Each objFile In FSO.GetFolder(DesktopPath)

Open in new window


to this
For Each objFile In FSO.GetFolder(DesktopPath).Files

Open in new window


Rob.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39785271
That worked thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39786897
No problem. Thanks for the grade.

Rob.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question