2

I am trying to delete all my temp files by running below batch file. but unfortunately, i am receiving "Access is denied." error. I am not sure on the root cause or how to minimize this.

@echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

Also suggest a way to delete all internet temp files(IE).

2
  • 2
    It's common for a few temporary files to be locked by a currently running process. The best thing to do is just skip those and delete everything else. IE has built in functionality to delete it's own temp files. It's highly recommended you use a more secure and performant browser, like Firefox or Chrome, though.
    – jpmc26
    Sep 25, 2016 at 7:53
  • @jpmc26 : I am able to delete those files manually, but when i run the batch file it says "Access is denied". So i don't think these files are engaged by any running process. Sep 25, 2016 at 8:22

1 Answer 1

3

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%\*
2
  • C:\Users\<user>\AppData\Local\Temp\hsperfdata_Appy\3832 - Access is denied. C:\Users\<user>\AppData\Local\Temp\FXSAPIDebugLogFile.txt The process cannot access the file because it is being used by another process. Sep 25, 2016 at 8:30
  • 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. Sep 25, 2016 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.