我有一个包含许多子文件夹的目录。我想删除所有文件,但保留每个子文件夹的前6个文件。如何在Windows Server 2012上批量执行此操作?
答案 0 :(得分:2)
BaseDir
和KeepFiles
以满足您的需求。:: Q:\Test\2018\05\02\SO_50129236.cmd
@Echo off & SetLocal
Set "BaseDir=Q:\Test\2018"
Set "KeepFiles=6"
Pushd "%BaseDir%" || (echo couldn't find BaseDir&Pause&exit /b 1)
For /F %%A in (
'dir /B /S /AD 2^>nul'
) do For /F "skip=%KeepFiles% delims=" %%B in (
'dir /B /A-D /ON "%%~fA\*" 2^>NUL'
) do Echo del "%%~fA\%%~nxB"