"访问被拒绝."尝试使用批处理文件删除TEMP文件时出错 [英] " Access is denied." error while trying to delete TEMP files using batch file

查看:150
本文介绍了"访问被拒绝."尝试使用批处理文件删除TEMP文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过在批处理文件下面运行来删除所有临时文件.但不幸的是,我收到访问被拒绝"的信息.错误.我不确定根本原因还是如何将其最小化.

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 *

还建议一种删除所有Internet临时文件(IE)的方法.

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

推荐答案

您的操作方式确实很危险:

The way you're doing it is really dangerous:

cd会更改目录,但不会更改驱动器.因此,如果您从驱动器D:运行此脚本,它将更改驱动器C的当前目录,然后继续删除您的D:驱动器中的所有内容.

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.

您可能需要cd /D %TEMP%来使其安全,但是最好避免更改当前目录:

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.

我只是运行了这个脚本,现在我的临时目录中有3个目录和10个文件(以前有很多旧文件)

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)

要删除临时IE文件,由于目录是隐藏的,因此略有不同,因此我们必须使用特殊的dir命令列出隐藏的目录(FOR命令看不到隐藏的目录)

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%\*

这篇关于"访问被拒绝."尝试使用批处理文件删除TEMP文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆