Problem deleting contents of the documents folder RRS feed

  • Question

  • I am trying to delete the contents of the user folders when the user logs in. This is for a public computer.

    The script I am running will delete from "Desktop, Downloads, Music, Pictures, and Videos" but not the "Documents" folder. The items I am testing with consists of:

    New Folder
         New Folder
              New Document
         New Document
    New Document

    My Script

    Const DeleteReadOnly = TRUE
    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    Dim uncPath(7), i, strPath(7)

    strPath(1) = "Desktop"
    strPath(2) = "Documents"
    strPath(3) = "Downloads"
    strPath(4) = "Music"
    strPath(5) = "Pictures"
    strPath(6) = "Videos"

    for i= 1 to 6
    uncPath(i) = strHomeFolder & "\" & strPath(i) & "\*"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'on error resume next
    for i = 1 to 6
    objFSO.DeleteFile(uncPath(i)), DeleteReadOnly
    objFSO.DeleteFolder(uncPath(i)), DeleteReadOnly

    I get the error message

    Line: 24
    Char: 2
    Error: Permission denied
    Code: 800A0046
    Source: Microsoft VBScript runtime error

    • Moved by Bill_Stewart Wednesday, September 4, 2019 9:22 PM This is not "scripts on demand"
    Thursday, April 18, 2019 8:35 PM

All replies

  • You cannot use wildcards with the FSO.  You must enumerate the files one at a time.

    Use PowerShell. It is easier


    Thursday, April 18, 2019 9:17 PM
  • The error is pretty obvious <Permission denied> !!

    You should not use VBScript anymore. Use Powershell instead.

    When you post code, please format it as code using the code posting tool provided on the icon bar of the post editor (second to last icon).


    Live long and prosper!


    • Edited by BOfH-666 Thursday, April 18, 2019 9:22 PM
    Thursday, April 18, 2019 9:21 PM