How To Automatically Delete an Icon or Folder from the Desktop

Article translations Article translations
Article ID: 197425 - View products that this article applies to.
This article was previously published under Q197425
Expand all | Collapse all

On This Page

SUMMARY

This article contains a sample VBScript file that automates the deletion of files or folders from the desktop.

Please note that if you are using Windows 95, you need to install WSH.exe from the following Web site to enable Windows Script Host:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx

MORE INFORMATION

Administrators can automate the deletion of files and or folders by adding a line similar to the following in the login script:
   Cscript \\<server>\<share>\RemoveIt.VBS //T:5 //B
				
Where <server> is the name of the server and <share> is the name of the share.

//T:5 Terminates the script after 5 seconds if it is still running.

//B Tells the script to run in non-interactive mode.

Sample Code

Using a text editor, create a VBScript file called RemoveIt.vbs with the following lines:
   '-----------------------------------------------------------------------
   ' For Windows 95 Windows Script Host must be installed.
   ' The following VBScript sample will delete a file called Sample.txt and
   ' a folder called "Sample Folder" from the desktop.
   ' NOTE: To view the results of this script create a file called
   ' "Sample.txt" and a folder called "Sample Folder" on your desktop
   ' before you run the script.
   ' ----------------------------------------------------------------------
   Dim WSHShell, DesktopPath
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   DesktopPath = WSHShell.SpecialFolders("Desktop")
   ' ON ERROR RESUME NEXT prevents error messages from appearing.
   ' Useful if the file or folder might have already been removed.
   on error resume next
   Icon = DesktopPath & "\sample.txt"
   Folder = DesktopPath & "\sample folder"
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set A = fs.GetFile(Icon)
   A.Delete
   set B = fs.GetFolder(folder)
   B.Delete
   WScript.Quit
				

REFERENCES

For additional information on Windows Script Host and VBScript Language Reference, please see the following Web site:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx

Properties

Article ID: 197425 - Last Review: August 8, 2007 - Revision: 3.3
APPLIES TO
  • Microsoft Visual Basic, Scripting Edition 3.0
  • Microsoft Windows 95
  • Microsoft Windows 98 Standard Edition
  • Microsoft Windows NT 4.0
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.5
Keywords: 
kbautomation kbhowto KB197425

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com