Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Restructure VB Script

Posted on 2014-01-14
5
Medium Priority
?
234 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

618 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