0

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"
Nick S
  • 3
  • 2
  • And what's the question about your code? Do you want to search and delete the 3 files in all that folders? – Andre Kampling Sep 07 '17 at 12:43
  • Hi Andre, thanks for the reply. I need to find a way to only delete the three files if they exits. Nothing more, nothing less. Was trying some for loops and if statements but couldn't find so far "the solution". – Nick S Sep 07 '17 at 12:45
  • 1
    You can also replace all instances of `C:\Users\%USERNAME%\AppData\Local` with `%LocalAppData%` and any instance of `C:\Users\%USERNAME%\AppData\Roaming` with `%AppData%` – Compo Sep 07 '17 at 13:51

1 Answers1

0

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:

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47