B_S
B_S

Reputation: 11

Command line loop through directories

I'm trying to write a batch file that will be run on a parent directory and will loop through subdirectories removing all but the newest 3 files from each subdirectory. What I have now recurses through the subdirectories but only skips the 3 newest files that it comes across, not the three newest files in each subdir. I think I need another loop in the code, but need help with where and what it should be. Help!

What I have so far - just ECHOing the output for now as a test.

@echo off
pushd "%~1"
for /f "skip=3 delims=" %%F in (
    'dir /s /a-d /o-d /b') do ECHO del "%%F" /f
popd

Upvotes: 1

Views: 2312

Answers (1)

Endoro
Endoro

Reputation: 37569

You might try this:

@echo off
pushd "%~1"
for /D %%i in (*) do (
   pushd "%%~i"
   for /f "skip=3 delims=" %%F in (
       'dir /a-d /o-d /b') do ECHO del /f "%%~F"
   popd
   )
popd

Upvotes: 1

Related Questions