F
F
frrykt2020-02-20 17:03:50
PowerShell
frrykt, 2020-02-20 17:03:50

How to delete files when a certain number of them (with verification) is exceeded by a script?

Good afternoon. It is necessary to leave the last 7 files in the folder by the date of creation, and delete the rest, but before that, check that the folder really has at least 7 files. How to remove it seems clear, how to calculate, too, seems to understand, but I can’t put it all together. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wisgest, 2020-02-20
@frrykt

pushd d:\test
for /f "skip=7 delims=" %%I in ('dir /b /a-d /o-d /tc') do  del "%%I"
popd

E
Eugene, 2020-02-20
@BeatHazard

On Powershell I threw something like this:
$files = Get-ChildItem -Path "c:\test" | Sort-Object creationtime -Descending
$count = $files.count - 7
if ($files.Count -igt 7)
{$DelFiles = $files | Select-Object -last $count
Remove-Item -Path $DelFiles.fullname}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question