Deleting folder for each user

I need to iterate thru user folders located in C:\Users and delete Chrome cache folders.  For example, I need to delete:

C:\Users\john.smith\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*

But for each user.  What would be the best way to accomplish this?  I'm thinking either a batch file, VBScript or VB program.
BrianAsked:
Who is Participating?
 
zerihCommented:
Yes it's possible in scripting languages.



Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Users"), 3 

Sub ShowSubFolders(Folder, Depth)
    If Depth > 0 then
        For Each Subfolder in Folder.SubFolders           
           ' CHECK IF THE CACHE FOLDER EXISTS AND DELETE IT
            Wscript.Echo Subfolder.Path
            ShowSubFolders Subfolder, Depth -1 
        Next
    End if
End Sub

Open in new window

0
 
zerihCommented:
I would personally use C# because it's industry standard but that's debatable and for a different topic. With the given options, I would use VB because I am a developer. It's really up to the person tasked with the job.

Imports System.IO

Module Module1

    Sub Main()
        For Each Dir As String In Directory.GetDirectories("c:\del\users")

            DelFolder(Dir)
        Next
    End Sub

    Sub DelFolder(subPath As String)
        Try
            Dim filePath = subPath + "\Cache\"
            Dim directoryExists = Directory.Exists(filePath) ' OR \AppData\Local\Google\Chrome\User Data\Default\Cache\ in your case
            Console.WriteLine("Does " + filePath + "sub directory exists: " & directoryExists)
            ' DELETE THE DIRECTORY
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module
0
 
BrianAuthor Commented:
Thanks Zerih!  Would it be possible to convert this to a VBScript?  I can do it in VB, but a script would be easier to update on the server if we ever want to make changes (like add other deletions).
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MacroShadowCommented:
Please try:
on error resume next

dim count

Set ws = WScript.CreateObject("WScript.Shell")
Set fs = WScript.CreateObject("Scripting.FileSystemObject")

rootFolder = "C:\Users\"

wscript.echo "Start"
count = 0

SearchAndDelete (rootFolder)

wscript.echo "Finished. Deleted " & count & " Chrome Cache folders & " from " & rootFolder

Sub SearchAndDelete(path)
	Set folderCollection = fs.GetFolder(path)
	For Each Subfolder in folderCollection.SubFolders
		subfolderPath = "C:\Users\" & Subfolder.Name & "\AppData\Local\Google\Chrome\User Data\Default\Cache\"
		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
	Next
End sub

Open in new window

1
 
Bill PrewCommented:
All you need is a simple BAT script like below.  Remember to run from elevated / admin privs.

@echo off
setlocal

set CacheDir=AppData\Local\Google\Chrome\User Data\Default\Cache

for /d %%U in ("c:\users\*.*") do (
    if exist "%%U\%CacheDir%\" (
        echo Removing "%%U\%CacheDir%\"
        rmdir /s /q "%%U\%CacheDir%\"
    )
)

Open in new window


»bp
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Using PowerShell

$userProfiles = get-childitem "c:\users";
foreach ($userProfile in $userProfiles)
{
    $folder = "C:\Users\" + $userProfile + "AppData\Local\Google\Chrome\User Data\Default\Cache";
    Remove-Item $folder -recurse -Force -erroraction silentlycontinue;
}

Open in new window

1
 
BrianAuthor Commented:
Thank you all for the help!  I appreciate all the options!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.