使用批处理脚本查找和删除驱动器上每个文件夹中的desktop.ini文件

时间:2013-09-30 10:26:05

标签: windows batch-file hidden delete-file

我想使用Windows批处理文件查找并删除网络驱动器H:上的每个desktop.ini和Recycle Bin.BIN文件。我目前有这个:

@echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
@pause
@H:
@for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
@for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
@echo Deleting complete, press any key to exit.
@pause

哪个适用于子文件夹中的任何文件,如果名称中有空格,则会出现“无法找到文件”错误。有任何建议如何解决?

4 个答案:

答案 0 :(得分:8)

对我有用的解决方案是

创建bat文件“delete_all_desktop_ini.bat” 与

del /s /q /f /a ".\desktop.ini" 

将其放入文件夹并运行

它将删除此文件的当前目录和子目录中的所有桌面inis。

我把它放在谷歌驱动器的项目文件夹中

答案 1 :(得分:6)

给这个测试:

我已将回收站名称更改为我在Windows 8中看到的名称。 名称随Windows的不同版本而变化。

@echo off
del /s /q /f /a "h:\desktop.ini" 
del /s /q /f /a "h:\$Recycle.Bin\*.*"

答案 2 :(得分:1)

出现此问题是因为默认空间是for命令的分隔符,但您可以使用delims选项更改此设置。如果您选择一个不会出现在文件路径中的字符,那么它应该可以正常工作:

@echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
@pause
@H:
@for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
@for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
@echo Deleting complete, press any key to exit.
@pause

答案 3 :(得分:0)

for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"

尝试使其正常工作从脚本中删除echo