No announcement yet.

Script to delete userprofile contents at login

  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to delete userprofile contents at login

    Hello I googled and found a script posted by Rems to delete my documents and desktop while keeping the shortcuts lnk files in tact.

    I wanted it to delete all files under a users profile (My Documents, My Videos, My Pictures, My Downloads, My Desktop, etc) so I changed the deletestuff and const lines to user profile instead of my documents and desktop so it now looks like this

    Const CSIDL_USER_PROFILE      = &H28&
    Dim fso: Set fso = CreateObject("scripting.filesystemobject")
    ' This script deletes subfolders with
    ' everything in it from the specified folders.
    ' Then it will delete the files with the
    ' exception of shortcuts and link files.
    With CreateObject("Shell.Application")
       DeleteStuff .Namespace(CSIDL_USER_PROFILE)
    End With
    wscript.quit 0
    sub DeleteStuff(ByVal objSpecial)
       Dim sItem, arrTokens, strExn
       on error resume next
       For Each sItem in objSpecial.Items
           If fso.FolderExists(sItem.Path) Then
           End If
           If fso.FileExists(sItem.Path) Then
             arrTokens = split(sItem.path, ".")
             strExn = LCase(arrTokens(ubound(arrTokens)))
             If strExn <> "lnk" _
               AND strExn <> "url" _
                 Then fso.DeleteFile(sItem.Path)
           End If
    End Sub
    This works to delete all the files added under the users profile but it also is deleting the desktop shotcuts.

    How do I modify this script to exclude the lnk files?

    Last edited by Rems; 18th July 2013, 22:03.

  • #2
    Re: Script to delete userprofile contents at login

    Anybody can help?


    • #3
      Re: Script to delete userprofile contents at login

      Not sure what you are trying to accomplish.

      Deleting files only under My Documents, My Videos, My Pictures, My Downloads, My Desktop, etc , is not the same as deleting everything under %USERPROFILE% what the script is doing now.
      The script will only apply to %USERPROFILE% directory of the account that's running the script, so you created a kamikaze script?

      With the original script, the content of the My_Documents folder and the DESKTOP of the user are cleared. It removes all files and subfolders. Shortcuts that are in the root of the specified folder are excluded.

      If your script should exclude shortcut files, also if the shortcut file is in a subfolder of the specified folder, you can not use the "DeleteStuff" subroutine.
      You need to create a function to iterate over all subfolders in the specified root folder - delete all files but not shortcut files in each folder.

      note - there are now three types of shortcut extensions:
      *.lnk, *.url and *.website

      Last edited by Rems; 21st July 2013, 20:27.

      This posting is provided "AS IS" with no warranties, and confers no rights.


      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts