Find and delete desktop.ini files in every folder on a drive using a batch script
Asked Answered
M

5

6

I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:

@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

Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?

Macario answered 30/9, 2013 at 10:26 Comment(0)
I
16

solution that worked for me was

create bat file "delete_all_desktop_ini.bat" with

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

put it in a folder and run it

it will delete all desktop inis in current directory and sub directories of this file.

i put it in a project folder that is in google drive

Ionization answered 14/4, 2016 at 12:9 Comment(0)
T
9

Give this a test:

I've altered the recycle bin name to what I see here in Windows 8. The name changes with different versions of Windows.

@echo off
del /s /q /f /a "h:\desktop.ini" 
del /s /q /f /a "h:\$Recycle.Bin\*.*"
Textuary answered 30/9, 2013 at 11:37 Comment(1)
Or del /s /q /f /a desktop.ini is the current folder.Watch
Y
1

The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:

@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
Yellow answered 30/9, 2013 at 11:13 Comment(0)
T
0
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"

Try it, to make it working remove echo from the script.

Trencher answered 30/9, 2013 at 12:29 Comment(0)
S
0

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

it should works as charm save file .bat put it in any folder it will delete ini files in folders and sub folders

Swiss answered 2/8, 2022 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.