The way you're doing it is really dangerous:
cd
without /D
changes directory but not the drive. So if you run this script from drive D:
it will change the current directory of drive C then will proceed deleting everything in your D:
drive.
If you are lucky, you don't have permissions on your other drive. If you're not you lose all your files.
You would have needed cd /D %TEMP%
to make it safe, but it's even better to avoid to change current directory:
@echo off
for /d %%D in (%TEMP%\*) do rd /s /q "%%D"
del /f /q %TEMP%\*
Of course, temp cleanup can fail because some files are in use by running programs. In that case, just reboot and run the script again.
I just ran this script and now there's 3 dirs and 10 files in my temporary directory (there were a lot of old files before)
To delete temporary IE files, it's slightly different as the directories are hidden so we have to list hidden dirs with a special dir
command (the FOR
command does not see the hidden directories)
@echo off
set IETEMP=%LOCALAPPDATA%\Microsoft\Windows\INetCache
for /F %%D in ('dir /AHD /B %IETEMP%') do rd /s /q "%IETEMP%\%%D"
del /f /q %IETEMP%\*