Batch to Delete Old Files In Subdirectories

March 28, 2011 at 05:44:25
Specs: Windows XP
Hello,
I'm looking for a batch script to go through a certain directory and preserve the 10 newest files while deleting the rest. This is what i have found to work on a single folder and I am having trouble figuring out how to nest it.

@echo off &setlocal

set folder=C:\test\

pushd "%folder%"


for /f "skip=10 tokens=*" %%a in ('dir /a:-d /b /o:-d') do del %%a


popd

pause

To Sum Up, I can get this script to run in a single folder but not any of the sub folders in that folder, how can this be done?


See More: Batch to Delete Old Files In Subdirectories

Report •

#1
March 28, 2011 at 16:30:28
try this ..


===========================================================
@echo off &setlocal
set folder=d:\subhash\script
cd /d %folder%
for /f %%b in ('dir /a:d /b') do (
pushd "%%b
for /f "tokens=*" %%a in ('dir /a:-d /b /o:-d') do echo del %%a
popd
)
pause
==========================================================


Before actual Del command i have added a echo. It will show you the actual command which will executed ..If u r satisfied with that then remove Del.

Subhash Chandra.


Report •
Related Solutions


Ask Question