?
Solved

Delete all the Network Places added through VBS

Posted on 2008-06-09
1
Medium Priority
?
1,101 Views
Last Modified: 2009-12-16
Hi,

I need a script able to delete network shares, I'm working on this script but I having a hard time on it, I need to delete all the existing shortcuts under my network places and create new ones, any ideas?

Thanks in advance.
Dim objFSO, strNetHood
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strNetHood = WshShell.SpecialFolders("Nethood")
 
'Delete "My Network Places" shortcuts
strDescription = "*"
objFSO.DeleteFile (strNetHood & "\\" & strDescription & ".lnk") 
 
 
Const NETHOOD = &H13&
 
Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")
 
Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path
 
strShortcutName = "Data"
strShortcutPath = "\\sfp034\sources"
 
Set objShortcut = objWSHShell.CreateShortcut _
    (strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save 
 
strShortcutName = "systems"
strShortcutPath = "\\intra.banesco.com\data\central"
 
Set objShortcut = objWSHShell.CreateShortcut _
    (strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save

Open in new window

0
Comment
Question by:pcfreaker
[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
1 Comment
 
LVL 11

Accepted Solution

by:
pcfreaker earned 0 total points
ID: 21753470
I solved this way.
Dim strPath, strName, strAppname,  blnDeleteMode,  objWSHShell, objShell, objFolder, objFolderItem, blnVerboseMode 
blnDeleteMode = True
Const NETHOOD = &H13& 
 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objWSHShell = CreateObject("Wscript.Shell") 
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.Namespace(NETHOOD) 
Set objFolderItem = objFolder.Self 
Dim oFolder, oSubFolders, oCurrentSubFolder 
 
Call DeleteResource()
Call CreateResource()
WScript.Quit
 
 
 
Sub DeleteResource()
 
'Deletes the previously created resources
		strPath = objFolderItem.Path & "\*" 
		On Error Resume Next 
		objFSO.DeleteFile strPath, true 
		
		'Si hay Error - Creates an event log
		If Err.Number <> 0 then 
		objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & "Cannot empty folder of files:" & strPath & vbNewLine & "Error detail:" & Err.Description 
		End If 
		
		'Some shortcuts are folders, loop and delete all of them too 
		Set oFolder = objFSO.GetFolder(objFolderItem.Path) 
		Set oSubFolders= oFolder.SubFolders 
		
		For Each oCurrentSubFolder in oSubFolders 
		On Error Resume Next 
		objFSO.DeleteFolder oCurrentSubFolder, true 
		
		'Si hay Error - Creates an event log 		If Err.Number <> 0 then 
		objWSHShell.LogEvent 1, strAppname & " error:" & vbNewLine & "Cannot empty folder of subfolders:" & strPath & vbNewLine & "Error detail:" & Err.Description 
		End If 
		Next 
		
End Sub
 
	Sub CreateResource()
			Dim objFSO, strNetHood, Cantidad, Title
			Const ForReading = 1
 
			Set WshShell = WScript.CreateObject("WScript.Shell")
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objNetwork = CreateObject("WScript.Network")
			strNetHood = WshShell.SpecialFolders("Nethood")
			
			Set objWSHShell = CreateObject("Wscript.Shell")
			
			Set objShell = CreateObject("Shell.Application")
			 
			Set objFolder = objShell.Namespace(NETHOOD)
			
			Set objFolderItem = objFolder.Self
			strNetHood = objFolderItem.Path
	
 
' 1st we must create the WSHShell object to interact
' with the user (show dialog and so on)
 
Set WSHShell = WScript.CreateObject("WScript.Shell")
' InputBox (prompt, title, default, xpos, ypos)
' prompt: the text shown in the input box
Title= "Input the ammount of resources to create"
' default: the value shown as default in the input field
' xpos/xpos: upper left corner of the input box
Ammount = InputBox(Message,Title,"0", 50, 10)
If Ammount = "" Then
WScript.Echo "cancelled by the user"
WScript.Quit
End If
 
Do While Cantidad >0
Cantidad= Cantidad - 1 
 
Title= "Input the path to create"
Set WSHShell = WScript.CreateObject("WScript.Shell")
Name = InputBox(Message,Title,"0", 50, 10)
 
Title= "Use an UNC: \\server\folder"
Set WSHShell = WScript.CreateObject("WScript.Shell")
Path = InputBox(Message,Title,"0", 50, 10)
 
'	Create Resource	
	strShortcutName = Name
	strShortcutPath = Path
	 
	Set objShortcut = objWSHShell.CreateShortcut _
	    (strNetHood & "\" & strShortcutName & ".lnk")
	objShortcut.TargetPath = strShortcutPath
	objShortcut.Save 
Loop

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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