I found some code to delete folders, in this case deleting all but n # of folders. I created 10 test folders, plus 1 that was already there. I want to delete all but 4. The code works, it leaves 4 of my test folders, except that it also leaves the other folder. Is there some attribute of the other folder that s getting checked in the batch file that s stopping it from getting deleted ? It was created through a job a couple of weeks ago.
Here s the code I stole (but don t really understand the details):
rem DOS - Delete Folders if # folders > n
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=4
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:MyFolder_TempFolderTest
If Exist "%temp% f}1{" Del "%temp% f}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
For /F "tokens=* skip=%_NumtoKeep%" %%I In ( dir "%_Path%" /AD /B /O-D /TW ) Do (
If Exist "%temp% f}1{" (
Echo %%I:%%~fI >>"%temp% f}1{"
) Else (
Echo.>"%temp% f}1{"
Echo Do you wish to delete the following folders?>>"%temp% f}1{"
Echo Date Name>>"%temp% f}1{"
Echo %%I:%%~fI >>"%temp% f}1{"
))
PopD
If Not Exist "%temp% f}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp% f}1{" | More
Set _rdflag= /q
Goto _Removeold
Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ( type "%temp% f}1{" ) Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%J")
:_Done
If Exist "%temp% f}1{" Del "%temp% f}1{"