fuuman
fuuman

Reputation: 461

batch - delete folders older than 10 days

i wrote a batch file that backup folders from a pc. The backup is stored on a server, for example \server\backup\pc1\folder1\today Now the backup script are running every day one time. At the 11th day its hast eleven subfolder unter folder1. now every day, when he backup again and create a folder eleven it should delete automatically the oldeset one, so that there are ten backup folders again.

I tried to make that with forfiles.exe (integrated in windows). but it didn't work that great with complete folders.

Can you help me?

Thanks!

Upvotes: 0

Views: 1034

Answers (2)

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions disabledelayedexpansion

    pushd "d:\somewhere\backups" && (
        for /f "skip=10 delims=" %%a in (
            'dir /b /ad /tc /o-d'
        ) do echo rmdir /s /q "%%~fa"
        popd
    )

It just changes to the indicated folder, get a descending creation date list of the folders, skip the first ten and remove the rest

rmdir commands are only echoed to console. If the output is correct, remove the echo command

Upvotes: 1

mickm
mickm

Reputation: 391

Which version of Windows are you using? This works with XP & above:

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S.

For more info, see here: http://www.computerhope.com/rmdirhlp.htm

Upvotes: 0

Related Questions