Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Help needed with vbscript to delete all files and folders on desktop with exclusions


Recommended Posts

Posted

I'm very new to scripting but at work my director challenged me with writing a script that would scrub the desktop and my documents for any file and folder and delete it, however it needs to keep the shortcuts on the desktop.

 

I've peiced together some parts of other scripts I think do the job for the most part, but I need help understanding on how to exclude the .lnk files to be deleted... I was told that I would need to use an array and getfile function to make this work... please help!

 

*****************

On Error Resume Next

strPath = "c:\Documents and Settings\user\Desktop"

Dim sCurrentDir

Dim objShell

 

 

Set objShell = CreateObject("Wscript.Shell")

sCurrentDir = objShell.CurrentDirectory

 

 

Function DeleteFolder(strFolderPath)

Dim objFSO, objFolder

 

 

Set objFSO = CreateObject ("Scripting.FileSystemObject")

If objFSO.FolderExists(strFolderPath) Then

objFSO.DeleteFolder strFolderPath, True

 

 

End if

Set objFSO = Nothing

msgbox "Desktop Reset Complete."

End Function

Posted

my update.... But running into problems with it not deleting everything in the My Pictures, My Videos, and My Music.... any suggestions?

 

******************

 

Dim sFileList()

Dim oFolder

Dim oFile

Dim objFSO

Dim sFileExtension

Dim sMessage

Dim objShell

Dim strPath

 

 

'// Run the cleanfolder function for the current directory i.e., desktop

Set objShell = CreateObject ("WScript.Shell")

sDesktop = objShell.SpecialFolders("Desktop")

msgbox sDesktop

CleanFolder sDesktop

 

 

'// Run the cleanfolder function for My Documents

sMyDocuments = objShell.SpecialFolders("MyDocuments")

msgbox sMyDocuments

CleanFolder sMyDocuments

 

 

sub CleanFolder (sFolderName)

Set objFSO = CreateObject ("Scripting.FileSystemObject")

 

 

Set oFolder = objFSO.GetFolder(sFolderName)

 

 

'// We are looping through all the files we found in the folder

For Each oFile In oFolder.Files

 

 

'// We are getting the file extension

sFileExtension = Right(oFile.name, 3)

 

'// We are checking if it is not a shortcut

If sFileExtension <> "lnk" and sFileExtension <> "vbs" Then

'// It's not a shortcut, so delete it

'oFile.Delete

msgbox "We'll delete " & oFile.Name

End IF

 

 

'// Loop to the next file in the Files collection

Next

 

 

Set objSubFolder = oFolder.SubFolders

 

 

For each oSubfolder in objSubFolder

 

's = oSubfolder.name & vbCrLf

'msgbox s

'oSubfolder.Delete

msgbox "We'll delete " & oSubfolder.Name

 

Next

End sub

Posted
I'm very new to scripting but at work my director challenged me with writing a script that would scrub the desktop and my documents for any file and folder and delete it, however it needs to keep the shortcuts on the desktop.

Does it have to be a VBS, because if not, PowerShell would be so much easier...

 

Get-ChildItem -Path $env:USERPROFILE\Desktop\* -Exclude *.lnk -Recurse | Remove-Item -Force -Recurse
Get-ChildItem -Path $env:USERPROFILE\Documents\* -Recurse | Remove-Item -Force -Recurse

 

or

 

Get-ChildItem -Path ([environment]::GetFolderPath("Desktop")) -Exclude *.lnk -Recurse | Remove-Item -Force -Recurse
Get-ChildItem -Path ([environment]::GetFolderPath("MyDocuments")) -Recurse | Remove-Item -Force -Recurse

 

Add -WhatIf onto the end of each line if you want to test it first without deleting any files.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...