Announcement

Collapse
No announcement yet.

Windows Logoff script/batch file to clear my documents and desktop on clients

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Windows Logoff script/batch file to clear my documents and desktop on clients

    Hi,

    I am currently looking for a logoff script or batch files that i can place in our client group policy. I need to clear the desktop and my documents of our client accounts at each logoff.

    I've found the following VB script on another forum. I don't know who wrote this so can't give credit for it. This currently clears the desktop when its run. I need this to be modified so it clears my documents as well.

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Shell.Application")
    Set wshShell = CreateObject("WScript.Shell")
    strDesktop = wshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\desktop"

    DeleteStuff strDesktop

    Function DeleteStuff(strFolder)
    Set objFolder = FSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    FSO.DeleteFile(objFile.Path)
    Next
    Set colSubFolders = objFolder.SubFolders
    For Each objSubFolder in colSubFolders
    DeleteStuff objSubFolder.Path
    Set objCheckFolder = objShell.Namespace(objSubFolder.Path)
    If objCheckFolder.Items.Count = 0 Then
    FSO.DeleteFolder(objSubFolder.Path)
    End If
    Next

    End Function

    Many thanks in advance for any assistance anyone can offer.
    Last edited by GM2011; 10th February 2011, 16:47.

  • #2
    Re: Windows Logoff script/batch file to clear my documents and desktop on clients

    strMyDocs = shShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\My Documents"
    DeleteStuff strMyDocs

    Try adding above two line of code before Function
    Thanks & Regards
    v-2nas

    MCTS 2008, MCTIP, MCSE 2003, MCSA+Messaging E2K3, MCP, E2K7
    Sr. Wintel Eng. (Investment Bank)
    Independent IT Consultant and Architect
    Blog: http://www.exchadtech.blogspot.com

    Show your appreciation for my help by giving reputation points

    Comment


    • #3
      Re: Windows Logoff script/batch file to clear my documents and desktop on clients

      Code:
      Const CSIDL_MY_DOCUMENTS = &H5
      Const CSIDL_DESKTOP      = &H10
      
      Set fso = CreateObject("scripting.filesystemobject")
      
      With CreateObject("Shell.Application")
         Set objMyDocuments = .Namespace(CSIDL_MY_DOCUMENTS)
         Set objDesktop = .Namespace(CSIDL_DESKTOP)
      End With
      
      DeleteStuff objMyDocuments
      DeleteStuff objDesktop
      
      wscript.quit 0
      
      
      Function DeleteStuff(ByVal objSpecial)
         On Error Resume Next
         Dim strList, icnt : icnt = 0   
         For Each objItem in objSpecial.Items
           icnt = icnt +1
           strList = strList & vbNewLine & objItem.name
      rem     fso.DeleteFile(objItem.Path)
      rem     fso.DeleteFolder(objItem.Path)
         Next
      
         '# check
         CreateObject("WScript.Shell").popup icnt & " Items deleted from" _
           & vbNewLine & objSpecial.Self.Path & ":" & vbNewLine _
           & strList, 3, "Thank you"
         strList = empty
      End Function
      The text in blue in the above sample is there for testing purpose.
      By removing the blue colored text it will make the script actually delete the items from the target folders.

      \Rems
      Last edited by Rems; 17th February 2011, 19:24.

      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

      Comment


      • #4
        Re: Windows Logoff script/batch file to clear my documents and desktop on clients

        This sample shows how you can exclude shortcuts and link files, if exist in the root of the specified folder(s), from being deleted.

        Code:
        Const CSIDL_MY_DOCUMENTS = &H5
        Const CSIDL_DESKTOP      = &H10
        
        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_MY_DOCUMENTS)
           DeleteStuff .Namespace(CSIDL_DESKTOP)
        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
                 fso.DeleteFolder(sItem.Path)
               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
        
           Next
        End Sub

        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

        Comment

        Working...
        X