Solved

Restructure VB Script

Posted on 2014-01-14
5
204 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That worked thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Thanks for the grade.

Rob.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now