none
Deleting a folder in all users profiles

    Question

  • Hi All,

    I have a VBScript that recursively searches all user profiles and deletes a folder that I specify, for the sake of it lets call it Example. The script works fine in XP but won't work in Windows 7.

    In XP, it will go to C:\Documents and Settings\ and scan through all user profiles and delete the Example folder which is exactly what I want.

    In Windows 7, it goes to C:\Users and checks C:\Users\Administrator and then exits without scanning any other profile. The strange this is, if I change the script to scan c:\temp and add profiles then it scans through them and deletes Example as required.

    Is there a trick to recursively scanning through c:\users? Below is the script that I am using (not my work, I am not capable of scripting such things) with the rootFolder set for Windows 7 C:\Users

    Cheers,
    Harry

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

    on error resume next

    dim folderSpec, count

    Set ws = WScript.CreateObject("WScript.Shell")

    Set fs = wscript.createobject("Scripting.FileSystemObject")


    rootFolder = "C:\Users\"

    folderSpec = "Example"

    wscript.echo "Start"
    count = 0

    deleteAllFolders (rootFolder)

    wscript.echo "Finished. Deleted " & count & " folders called " & folderSpec & " from " & rootFolder

    'Deletes all folders in 'rootFolder' that are called 'folderSpec'
    Sub deleteAllFolders(rootFolder)
    set folderCollection = fs.GetFolder(rootFolder)
    wscript.echo "Starting at folder: " & rootFolder
    for each subFolder in folderCollection.subFolders
    wscript.echo "Checking folder: " & subfolder.path
    RecursiveSearchAndDelete(subFolder.path)
    next
    end sub

    sub RecursiveSearchAndDelete(path)
    set folderCollection = fs.GetFolder(path)
    For Each Subfolder in folderCollection.SubFolders
    if subFolder.name = folderSpec then
    subfolderPath = subfolder.path
    wscript.echo "* Found: " & folderSpec & " at " & subfolderPath
    wscript.echo "* Deleting: " & subfolderPath
    set folderToDelete = fs.GetFolder(subfolderPath)
    folderToDelete.delete(true)
    if err.number <> 0 then
    wscript.echo "* An error occured deleting: " & subfolderPath
    else
    wscript.echo "* Deleted: " & subfolderPath
    count = count + 1
    Exit For
    end if
    end if
    RecursiveSearchAndDelete Subfolder
    Next
    end sub
    Tuesday, November 12, 2013 5:01 AM

Answers

  • I found a solution for this....

    As to run the command in Powershell was a single line, I ran it from a batch file. The batch file simply said Powershell and then the command and it ran without any problem or signed certificate etc.... was a very easy by-pass in the end but took me a long time to get to it.

    Thanks for the advice to those who helped.

    Wednesday, November 13, 2013 9:07 AM

All replies

  • Hi,

    you can use powershell to simplify this.

    • Get the list of directories in the c:\users directory

    $users = get-childitem c:\users

    • Loop through all the users folders

    foreach ($user in $users){

    • Set the folder you want to delete, for e.g., the temp internet files

    $folder = "C:\Users\" + $user + "\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook"

    • Delete the folder and any subfolders/files in it. Continue if there's any error.

    Remove-Item $folder -recurse -Force - erroraction silentlycontinue

    The whole script will be as follows:

    $users = Get-ChildItem C:\Users

    foreach ($user in $users){

    $folder = "C:\users\" + $user + "\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook"

    Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue

    }


    aravindg

    • Edited by aravindg Tuesday, November 12, 2013 5:46 AM
    Tuesday, November 12, 2013 5:44 AM
  • Thanks for the response however powershell scripts won't run as they are unsigned and I have no control over the group policies to allow them. 
    Tuesday, November 12, 2013 6:06 AM
  • there are workarounds if you are willing to try them.

    • You can enable running of scripts locally by setting the execution policy, and save the script text to a local .ps1 file. For this you will need admin rights on the machine
    set-executionpolicy remotesigned
    • you can copy the script text and directly paste it in the powershell console. You don't need to run it as a .ps1 script.

    aravindg

    Tuesday, November 12, 2013 6:35 AM
  • Unfortunately I need to be able to push this script out so manually doing things on individual machines is out. If I could call a powershell script it would work but if I can, I don't know how.
    Tuesday, November 12, 2013 10:02 PM
  • ... I have a VBScript that recursively searches all user profiles and deletes a folder that I specify, for the sake of it lets call it Example. The script works fine in XP but won't work in Windows 7.

    In XP, it will go to C:\Documents and Settings\ and scan through all user profiles and delete the Example folder which is exactly what I want.

    In Windows 7, it goes to C:\Users and checks C:\Users\Administrator and then exits without scanning any other profile. The strange this is, if I change the script to scan c:\temp and add profiles then it scans through them and deletes Example as required ...

    Hi,

        

    Based on the above script settings, it is capable of deleting C:\Users\<profile>\Example. One probable cause for not producing the expected result could be due to UAC issue. To verify, please try this on one machine:

    1. Click Start menu.
    2. Click All Programs followed by Accessories.
    3. Right click on Command Prompt and left click on Run as administrator.

    Run the script to verify that it is working as per normal. If it is working, it means that you need to execute this script using elevated rights. One suggestion is to run it as a scheduled task.

        

    Hope that helps.

        

    Cheers, 


    Tas Chew

    Wednesday, November 13, 2013 6:22 AM
  • Thanks for the reply. I have actually already tried that and it didn't work. I really do wonder whether Windows 7 has an unusual way of handling User accounts.

    Wednesday, November 13, 2013 9:04 AM
  • I found a solution for this....

    As to run the command in Powershell was a single line, I ran it from a batch file. The batch file simply said Powershell and then the command and it ran without any problem or signed certificate etc.... was a very easy by-pass in the end but took me a long time to get to it.

    Thanks for the advice to those who helped.

    Wednesday, November 13, 2013 9:07 AM