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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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 PrewIT / Software Engineering ConsultantCommented:
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 SpecialistCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.