简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM