0

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!

fuuman
  • 469
  • 2
  • 7
  • 19
  • Please read the following previous post regarding the same issue: http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days – mickm Sep 16 '14 at 13:19
  • I saw that article, but as I said. With folders it didn't work so fine.. – fuuman Sep 16 '14 at 13:45

2 Answers2

1
@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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This is really nice. But why dir /o-d? This means, that the oldest are on top of the list and he will delete the newest one?! Or I am thinking wrong? – fuuman Sep 16 '14 at 14:30
  • @fuuman, in ascending date order, the order is from older to newer. In descending date order, the list goes from newer to older. As the `for` will skip from the start of the list, we need the newer folders at the start. – MC ND Sep 16 '14 at 14:34
  • Ah okay, awesome. But what does this "%%~fa" mean? And how would the this line would be for deleting files instead of folders? 'do del /q "%%~fa"' didn't work for me.. :\ – fuuman Sep 17 '14 at 14:30
  • @fuuman, `%%~fa` is the full path to the element referenced by the `for` replaceable parameter `%%a`. Where do you see the `del` command? It is not in my code. If what you need to do is delete files, then `del` can do it, but you will need to change the `dir` command, as it is asking for only folders (`/ad`) – MC ND Sep 17 '14 at 14:36
  • I am using your code for folder and now I have the same problem with files, so I want to customize it. Ah okay, you're right. So dir /a-d would be the best I think. //edit: yes, it worked! :) – fuuman Sep 17 '14 at 14:39
0

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

mickm
  • 281
  • 2
  • 6
  • 20
  • Please read the question again carefully. Your answer does **not** answer the original question. It does not address deleting directories older than 10 days. – DavidPostill Aug 07 '15 at 10:45