Solved

Restructure VB Script

Posted on 2014-01-14
5
217 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to implement an additional command 16 45
pull links for all files in folder AND subfolder 15 53
using vb script 5 51
AD Tool/Script Populate Security Groups Automatically 8 40
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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