?
Solved

Restructure VB Script

Posted on 2014-01-14
5
Medium Priority
?
237 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
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 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 2000 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] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

840 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