For loop doesn't work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

For loop doesn't work

#1 Post by Docfxit » 26 Sep 2018 20:22

The for loop in my bat file doesn't work.
The bat file should delete all files named desktop.ini in all folders on the partition.

Code: Select all

@echo on

Set Drive=E:
%Drive%
attrib -r -s -h -a "desktop.ini" /s
@for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do (
	del /A:H "%%i"
	Echo "%%i"
	)
cd\

pause

Code: Select all

 INFO.BAT version 1.5
--------------------------------------------------------------------------------
Windows version        :  Microsoft Windows [Version 6.1.7601]
Product name           :  Windows 7 Ultimate, 32 bit
Performance indicators :  Processor Cores: 4      Visible RAM: 2604844 kilobytes

Date/Time format       :  (mm/dd/yy)  Wed 09/26/2018  19:14:26.59
__APPDIR__             :  C:\Windows\system32\
ComSpec                :  C:\Windows\system32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Disabled
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  en-US       Code Pages: OEM  437    ANSI 1252
DIR  format            :  03/18/2018  11:49 AM         3,959,016 ntoskrnl.exe
Permissions            :  Elevated Admin=Yes, Admin group=Yes


ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: For loop doesn't work

#2 Post by ShadowThief » 26 Sep 2018 21:24

You recursively unset the hidden attribute on all desktop.ini files on line 5, but then you explicitly specify to only delete hidden files on line 7. Get rid of the /A:H from your del statement and it should work.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: For loop doesn't work

#3 Post by Docfxit » 26 Sep 2018 23:25

That worked excellent. Thank you.

I have another For loop that isn't working:

Code: Select all

cd\
attrib -r -s -h -a "$Recycle.BIN" 
@for /f "usebackq delims=|" %%i in (`dir /s /b /x  ^| find "$Recycle.BIN"`) do (
	del "%%i"
	Echo "%%i"
	)
Docfxit

CirothUngol
Posts: 46
Joined: 13 Sep 2017 18:37

Re: For loop doesn't work

#4 Post by CirothUngol » 27 Sep 2018 17:48

$Recycle.Bin is a folder and cannot be removed using the DEL command. Besides, it's also a system folder that you probably don't want to remove.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: For loop doesn't work

#5 Post by Docfxit » 27 Sep 2018 21:33

I have tried to replace del with rd. It still doesn't work.
The Attrib removes the system attribute.
The reason I would like to remove it is because there are times when there is more than one file within the folder $Recycle.BIN.
When the folder $Recycle.BIN is removed Win7 recreates it automatically.

I have changed it to this:

Code: Select all

cd\
attrib -r -s -h -a "$Recycle.BIN" 
rd /s /q %Drive%\$Recycle.bin
It's working now.

Thank you,
Docfxit

Post Reply