Search code examples
windowsbatch-fileinternet-explorerflashcaching

.bat file for selectively deleting IE cache for three files


thanks for helping out

I currently face some issues while working on a .bat file that deletes the Internet Explorer 11 cache specifically for this three files:

  • Analytics.swf
  • Deal.swf
  • Pricing.swf

Currently I use the code attached, but it deletes all files in the cache (OS = Windows 10):

@echo off

set DataDir=C:\Users\%USERNAME%\AppData\Local\Microsoft\Intern~1\

del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"

set History=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\History

del /q /s /f "%History%"
rd /s /q "%History%"

set IETemp=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Tempor~1

del /q /s /f "%IETemp%"
rd /s /q "%IETemp%"

set Cookies=C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies

del /q /s /f "%Cookies%"
rd /s /q "%Cookies%"

C:\bin\regdelete.exe HKEY_CURRENT_USER "Software\Microsoft\Internet Explorer\TypedURLs"

Solution

  • The following batch script will loop through the 4 folders you provide and search for all three files (see how to loop through arrays in batch) in all subdirectories (dir /b /s /a:-d "%%~f").

    If the script is fine for you remove the echo in front of the del command:

    @echo off
    
    set "filesDel[0]=Analytics.swf"
    set "filesDel[1]=Deal.swf"
    set "filesDel[2]=Pricing.swf"
    
    set "dirDel[0]=%LocalAppData%\Microsoft\Intern~1"
    set "dirDel[1]=%LocalAppData%\Microsoft\Windows\History"
    set "dirDel[2]=%LocalAppData%\Microsoft\Windows\Tempor~1"
    set "dirDel[3]=%AppData%\Microsoft\Windows\Cookies"
    
    rem loop over all directories defined in dirDel array
    for /f "tokens=2 delims==" %%d in ('set dirDel[') do (
       if exist "%%~d" (
          pushd "%%~d"
          rem loop over all files defined in filedDel array
          for /f "tokens=2 delims==" %%f in ('set filesDel[') do (
             rem search for file and delete it
             for /f "tokens=*" %%g in ('dir /b /s /a:-d "%%~f"') do (
                echo del /f "%%~g"
             )
          )
          popd
       )
    )
    

    Note that I use the quotes in the set command like: set "name=content", this allows spaces in the path name without having the quotes in the variable content itself.


    Command reference links from ss64.com: