Page 1 of 1

For loop doesn't work

Posted: 26 Sep 2018 20:22
by Docfxit
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


Re: For loop doesn't work

Posted: 26 Sep 2018 21:24
by ShadowThief
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.

Re: For loop doesn't work

Posted: 26 Sep 2018 23:25
by Docfxit
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

Re: For loop doesn't work

Posted: 27 Sep 2018 17:48
by CirothUngol
$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.

Re: For loop doesn't work

Posted: 27 Sep 2018 21:33
by Docfxit
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