Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with script

Status
Not open for further replies.

Briandr

MIS
Jul 11, 2003
177
US
Option Explicit

Dim FSO : Set FSO = Createobject("Scripting.FileSystemobject")
Dim Shell : Set oShell = WScript.Createobject("WScript.Shell")
Dim AllUsersProfile : AllUsersProfile = Shell.ExpandEnvironmentStrings("%AllUsersProfile%")
Dim Target
Hi,

Can someone tell if this code is correct, I think it may be wrong. To start I want to delete an existing folder (and its shortcut inside of it) from the Windows 7 Programs menu. It appears at one time while messing with the code it deleted the shortcut, but not folder. Now I am getting some variable undefined error.

Target = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools"

If fso.FolderExists(target) Then
fso.DeleteFolder target
End If

Set ShellLink = oShell.CreateShortcut(AllUsersProfile & "\Microsoft\Windows\Start menu\Programs\Microsoft Office 2010 Tools\Snapshot Viewer.lnk")
oShellLink.TargetPath = "C:\Program Files (x86)\Snapshot Viewer"
oShellLink.WorkingDirectory = "C:\Program Files (x86)\Snapshot Viewer"
oShellLink.IconLocation = icon
oShellLink.Save

Thank you.





 
Hi,

I went back and re-worked this, but I still don't think it is working correctly. The folder I want removed is not being removed from the Windows 7 start menu and the new shortcut is not being created.

OON ERROR RESUME NEXT

DIM path, fso, WshShell, oShellLink, icon


'Set the File system Object
Set fso = CreateObject("Scripting.FileSystemObject")

SM = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office Tools\"

If fso.FolderExists("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools\") = True then 'Delete folder if it exists
fso.DeleteFolder("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools\")
End If

'Create Link
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut(SM & "Snapshot Viewer.lnk")
oShellLink.TargetPath = "C:\Program Files (x86)\Snapshot Viewer"
oShellLink.WorkingDirectory = "C:\Program Files (x86)\Snapshot Viewer"
oShellLink.IconLocation = icon
oShellLink.Save





 
First off, you're deleting a directory and then trying to create a shortcut in the deleted location. My experience has been that VBS is not smart enough to know that it needs to create the directory before creating the file and will, instead, throw an error (which you will not see b/c you've disabled error reporting).

For testing, I would suggest deleting (or commenting out) "On Error Resume Next", as this will only make it more difficult to troubleshoot. Then, instead of working in the C:\ProgramData directory, I would suggest creating a test folder on the root of the C: drive. If you can get your script to work in the test directory, then you know the code is working. Then, if it's not working in the C:\ProgramData directory, you probably need to run it with Admin credentials. I don't recall the exact circumstance, but I was trying to do something similar a while back, and eventually found that VBS wasn't powerful enough to do what I needed and that I had to switch to PowerShell. I can't say you'll face the same limitation, but it's something to be aware of if you can't get this to work in the ProgramData directory. Also, you didn't say, but I'm presuming you're on 64-bit Win7.
 
This is what I have now:

Set FS = CreateObject("Scripting.FileSystemObject")
SM = ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2010 Tools\")
oShellLink.TargetPath = "C:\Program Files (x86)\Snapshot Viewer\SNAPVIEW.EXE"
oShellLink.WorkingDirectory = "C:\Program Files (x86)\Snapshot Viewer"
If fs.FolderExists ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools") then
FS.CopyFile ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools\*.*"), ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2010 Tools\")
FS.DeleteFolder ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools")
Else
strShortcutName = "Microsoft Access Snapshot Viewer.lnk"
strStartmenuName = "Microsoft Office 2010 Tools\"
strShortcutWorkingDir = "C:\Program Files (x86)\Snapshot Viewer"
strShortcutTargetPath = "C:\Program Files (x86)\Snapshot Viewer\SNAPVIEW.EXE"
strStartMenuLocation = "c:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2010\"
End If

set WshShell = CreateObject("WScript.Shell")
set oMyShortcut = WshShell.CreateShortcut(strStartMenuLocation + strStartmenuName + "\" + strShortcutName)
oMyShortcut.TargetPath = strShortcutTargetPath
oMyShortcut.Arguments = strShortcutArguments
oMyShortcut.WorkingDirectory = strShortcutWorkingDir
wshshell.end

Any ideas as I am still getting object undefined error on line 3?

Thanks.
 
What is oShellLink ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I am not sure what the oShellLink was in reference to. I am modifying a script by a prior person. This new revision works, no errors but it does not place the shortcut in the correct folder

It should be under Programs\Microsoft Office\Microsoft Office 2010 Tools.
The shortcut appears under Programs\Microsoft Office and it appears as Microsoft Office Tools 2010Snapshot Viewer.

Any ideas on how to correct?

DIM PATH, fso, OS, strComputer, WshShell, oShellLink, icon

Set fso = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office Tools"
Fso.DeleteFolder strFolder

SM = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office Tools 2010"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut(SM & "Snapshot Viewer.lnk")
oShellLink.TargetPath = "C:\Program Files (x86)\Snapshot Viewer\SNAPVIEW.EXE"
oShellLink.WorkingDirectory = "C:\Program Files (x86)\Snapshot Viewer"
oShellLink.Save





 
Set oShellLink = WshShell.CreateShortcut(SM & "[!]\[/!]Snapshot Viewer.lnk")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top