📜 VBScript (WSH)

Clean Up The pip download cache (VBScript)

Reports the size of the pip download cache and deletes its contents after a confirmation prompt.

By WindowsScripting.com · 1.5 KB
CleanPipCache.vbs
' ============================================================
' CleanPipCache.vbs
'
' Reports the size of the pip download cache, then deletes its contents after
' confirmation.
'
' Usage: cscript CleanPipCache.vbs
' ============================================================

Option Explicit

Dim objFSO, objShell, objFolder, objFile, objSubFolder
Dim targetPath, totalSize, fileCount, confirm

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

targetPath = objShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") & "\pip\Cache"

If Not objFSO.FolderExists(targetPath) Then
    WScript.Echo targetPath & " does not exist on this machine."
    WScript.Quit
End If

Set objFolder = objFSO.GetFolder(targetPath)
totalSize = 0
fileCount = 0

On Error Resume Next
For Each objFile In objFolder.Files
    totalSize = totalSize + objFile.Size
    fileCount = fileCount + 1
Next
On Error Goto 0

WScript.Echo targetPath & " contains " & fileCount & " file(s), " & _
             Round(totalSize / 1024 / 1024, 2) & " MB"

confirm = MsgBox("Delete the contents of " & targetPath & "?", vbYesNo + vbQuestion, "Confirm cleanup")
If confirm = vbYes Then
    On Error Resume Next
    For Each objFile In objFolder.Files
        objFile.Delete True
    Next
    For Each objSubFolder In objFolder.SubFolders
        objSubFolder.Delete True
    Next
    On Error Goto 0
    WScript.Echo "Cleanup complete."
Else
    WScript.Echo "Cancelled."
End If