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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to clear My Network Places? 1

Status
Not open for further replies.

RubADub

Programmer
Sep 22, 2008
8
NO
Greetings all!

I'm quite new to programming and scripting, so please bear with me if I ask seemingly mundane questions, m'kay? :)

My employer has asked me to come up with a logon/startup script to use when setting up a new server at a branch office. Basically, the script should clean up any mappings referring to the old server on our employees desktop computers. In order to minimize possible headaches when changing servers like this, we've decided to name the new server with the same name as the old server. Typically such changing of servers occur when servers are past their designated end-of-life time, typically 4-5 years. Also storage might be a factor, or should I rather say a lack of storage.

I would like my script to clear all stored network locations under "My Network Places". That is, of course, everything except the standard "Entire Network" and so forth. How would I go about that using VBscript? If there's any other registry keys that come in to play, please let me know, so that I can look into it. Like I said, I'm quite new to this ;-)

We've noticed that if there are lots of network shares located here, it will slow down Explorer significantly, especially on CAD workstations. I reckon I'll alert the Desktop IT group about how to Prevent Network Share Shortcuts from Being Added to My Network Places, according to this MS KB article: but for the time being I'll have to come up with a script in order to clear stored shares.

I've done some searhing for VBScripts to clear My Network Places, but I've come up empty handed so far. I would greatly appreciate if someone could point me in the right direction :)

Regards
RubaDub
 
Hope this helps

Code:
Dim strNetHood, strMyDocuments

Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
	strNetHood = WshShell.SpecialFolders("Nethood")
	strMyDocuments = WshShell.SpecialFolders("MyDocuments")
   
   Dim sNethoodArray( 6, 1 )
		sNethoodArray( 1, 0 ) = "Name here"
		sNethoodArray( 2, 0 ) = "Name here"
		sNethoodArray( 3, 0 ) = "Name here" 	
		sNethoodArray( 4, 0 ) = "Name here" 
		sNethoodArray( 5, 0 ) = "Name here" 	
		sNethoodArray( 6, 0 ) = "Data"

		
    Dim strDescription, strUNCPath, nIdx
 For nIdx = 0 To UBound( sNethoodArray, 1 )
     strDescription = sNethoodArray( nIdx, 0 )
    objFSO.DeleteFile (strNetHood & "\" & strDescription & ".lnk") 
 Next
 
Thanks, GrimR! I'll have a gander at it, and run the script in a test environment.

Newbie question:
I see that you declare an array sNethoodArray. Seing as I'm not used to working with arrays, might I ask you what = "Name here" and = "Data" actually does here?
And is this array limited to size in any way, that is the number of stored network locations in NetHood? Sorry for my impatience, I'll have a closer look at the script, and try running it now.

Thank you very much for your prompt reply :)

 
= "Name here" and = "Data" actually does here? Just the name of the Network Neighborhood drives.
Not sure if you can use a instead wildcard here

e.g this will delete a single one

Code:
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" shortcut
strDescription = "Data"
objFSO.DeleteFile (strNetHood & "\" & strDescription & ".lnk")
 
It seems as though the script isn't working properly, though. I get a runtime error, no matter what strDescription I use.

Besides, I really need to use a wildcard of some sort, as I have no idea of knowing how many locations are stored under Nethood, and what their names are. Hmmmmmmm, this is looking to be more complicated than I initially had thought.

I appreciate your help, GrimR, it's much appreciated :)
 
Try deleting all .lnk files from the users nethood folder instead.

 
I wish I could, but it seems as though the entries under NetHood aren't shortcuts (*.lnk), but rather folders.

Issuing a command "del *.lnk" in NetHood accomplishes nothing, as there are no .lnk files.

Issuing a command "del *.*" doesn't remove the folders, either.

*scratch head*
 
"del *.lnk" in NetHood accomplishes nothing
FOR /D %D IN (*) DO RD /S/Q "%D"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm in over my head here.

I apologize for my shortcomings. This is all new to me, and I'm not capable of writing this script myself, and I do not have the knowledge required to add tidbits and whatnot to scripts I do not understand.

I thought this script wouldn't be too difficult to make, but I guess I proved myself wrong, as I am utterly confused. I fail.

I won't bother you all again. Thanks for your advice, though.
 
You give up too quick. Try this

Code:
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()
WScript.Quit
 
Sub DeleteResource()
 
'Deletes the previously created resources
		strPath = objFolderItem.Path & "\*" 
		On Error Resume Next 
		objFSO.DeleteFile strPath, true 
		
		'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 
		objFSO.DeleteFolder oCurrentSubFolder, true 
		
		'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 
		Next 
		
End Sub
 
Indeed I gave up too quick. I was having a moment of panic back there. Sorry for the drama. But I'm still considering my options regarding jobs, as my current job seems to be quite stressful. Well, when one is set to perform tasks one is not qualified for, stress tends to arise.

Anywho, I actually just found the same exact script you provided in your last post. How's that for coincidences. Experts-Exchange? Signing up for Experts-Exchange is one of the wisest decisions of my life.

Thank you so much for your advice! See, I'm happy again! :-D
(No, I am not manic depressive :p )
 
Sorry for the double post, but regarding the code snippet in your previous post I reckon it shouldn't be a problem removing the variables that aren't used by the DeleteResource procedure, right?

That is, since strName and blnVerboseMode aren't used by the procedure, there's no need to declare them.
 
Yes, I just edited a script, did'nt fully clean it up. You can even remove the writing to event log if you wish, don't see why you would need it.
 
Thank you once again, GrimR. You're a lifesaver :)

The script works fine the way it is, but I would like to run the script for selected users only, by doing a LDAP search and select users by Office Location.

I've modified the script with an if-statement, so that the procedure DeleteResource() only runs if the user is located at a certain office in Active Directory. Here it is, the changes are marked in green color:

Code:
Dim strPath, blnDeleteMode,  objWSHShell, objShell, objFolder, objFolderItem 
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 

[COLOR=green]
'--- LDAP search for employee
Set objNet = CreateObject("WScript.NetWork")
LDAPString="LDAP://cn=" & objNet.UserName & ",cn=users,dc=*,dc=com"
Set objAD = GetObject(LDAPString)

if objAD.l = "Office_Location" then[/color]
 
Call DeleteResource()
WScript.Quit

[COLOR=green]  
End if[/color]

 
Sub DeleteResource()
 
'Deletes the previously created resources
                strPath = objFolderItem.Path & "\*" 
                On Error Resume Next 
                objFSO.DeleteFile strPath, true 
                            
                '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 
                Next 
                
End Sub

I've tried running it, and it seems to working just fine. If I change the Office_Location to anything other than the office location I'm currently at, nothing happens, which I reckon is due to the DeleteResource() procedure not being called. Anyone care to comment?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top