Skip to content

Instantly share code, notes, and snippets.

@Waxolunist
Created February 22, 2012 15:23
Show Gist options
  • Select an option

  • Save Waxolunist/1885529 to your computer and use it in GitHub Desktop.

Select an option

Save Waxolunist/1885529 to your computer and use it in GitHub Desktop.
Delete all files in a directory except the n newest on Windows
:: ****************************************************************************
:: Delete all files except the n newest
:: ****************************************************************************
:: ----------------------------------------------------------------------------
:: Usage: delete_except_newest.bat DIR N FILTER
:: Example: delete_except_newest.bat C:\tmp 10 *.txt
:: ----------------------------------------------------------------------------
:: ****************************************************************************
:: ****************************************************************************
:: Init
REM CLS
@ECHO off
SetLocal EnableDelayedExpansion
:: ****************************************************************************
@ECHO.
:: ****************************************************************************
:: Variables
:: ****************************************************************************
SET DIR=%1%
SET /a N=%2%
SET FILTER=%3%
:: ****************************************************************************
REM list all filenames, except directories, sorted by date of creation, newest first
for /F "skip=%N% tokens=*" %%G in ('dir /T:C /O:-D /B /A:-D "%DIR%\%FILTER%"') DO DEL "%DIR%\%%G
@ECHO Program exited successfully.
:: ****************************************************************************
@Waxolunist
Copy link
Author

Schön wenn man helfen kann. 😊

@hueldoeu
Copy link

hueldoeu commented Sep 5, 2025

Wie könnte man delete_except_newest . 7 *.gz als Aufgabe in der Aufgabenplanung 1 Mal im Monat (vorerst, dann 1 Mal täglich, damit es immer 7 tar.gz-Archive bleiben) ausführen?

@Waxolunist
Copy link
Author

Da kenne ich mich mit Windows zu wenig aus. Aber hier sind ein paar gute Antworten https://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows

@hueldoeu
Copy link

damit klappt es

mklink /D "A:\charly-export-database-tar-gz-mo-mi" "A:\FULLBACKUP-MO&MI-CAUTION-DO-NOT-CHANGE\Solutio\DatabaseBackup\postgres"

in der Aufgabenplanung steht:
Programm/Skript: A:\charly-export-database-tar-gz-mo-mi\delete_except_newest.bat
Argumente hinzufügen (optional): A:\charly-export-database-tar-gz-mo-mi\ 7 *.gz

es geht hauptsächlich darum, den unmöglichen Pfad mit dem &-Zeichen über mklink zu umgehen.

man achte auf die Syntax von mklink https://superuser.com/a/597552

@hueldoeu
Copy link

hueldoeu commented Nov 11, 2025

solutio hat sein charly-internes Backup-Skript (zumindest wenn ich es manuell modifiziere und selbst Aufgaben mit selbstgeschrieben Befehlen in der Aufgabenplanung speichere) gebacken bekommen. Es sind nie mehr als 8 tar.gz Archive.

Es war mir eine Ehre 🦸‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment