+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Help needed with vbscript to delete all files and folders on desktop with exclusions in Coding and Web Development; I'm very new to scripting but at work my director challenged me with writing a script that would scrub the ...
  1. #1

    Join Date
    Sep 2013
    Location
    Colorado
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

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

    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

  2. #2

    Join Date
    Sep 2013
    Location
    Colorado
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

  3. #3


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    8,865
    Thank Post
    226
    Thanked 2,664 Times in 1,963 Posts
    Rep Power
    785
    Quote Originally Posted by Ddoan View Post
    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...

    Code:
    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

    Code:
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 4
    Last Post: 19th June 2013, 11:41 AM
  2. Users are able to see hidden files and folders
    By bigmouth in forum Windows
    Replies: 20
    Last Post: 26th March 2013, 12:47 AM
  3. Replies: 6
    Last Post: 30th November 2011, 03:12 PM
  4. Scripting to Delete multiple files and folders.
    By mdixon123 in forum Scripts
    Replies: 8
    Last Post: 2nd January 2011, 07:28 PM
  5. Replies: 4
    Last Post: 15th November 2006, 03:19 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •