I have configured Windows Backup and Restore function on Windows 7 Professional desktops to make daily backup on a network share placed on a Windows Server 2012. (BTW It's a pity that this utility does not have an option to use currently logged in user's credentials especially Active Directory user's credentials. Instead it is necessary to enter fixed user's credentials. Thus password will not update when changed in Active Directory.) This utility also does not have any maintenance mechanism for automatically removing old backup versions. So it will produce a lot of outdated data quickly filling server's disk space.
Now I have such a situation in directory structure (this is only for one user but there are many other users' folders in 'Users' catalog):
Users
├─agnes.microt
│ ├─AGNES-PC
│ │ ├─Backup Set 2014-02-24 060001
│ │ │ ├─Backup Files 2014-02-24 060001
│ │ │ ├─Backup Files 2014-02-25 091044
│ │ │ ├─Backup Files 2014-02-26 092458
│ │ │ └─Catalogs
│ │ └─Backup Set 2014-02-27 091933
│ │ ++ ├─Backup Files 2014-02-27 091933
│ │ ++ └─Catalogs
What I want to do is to delete (by run a batch file in a daily scheduled task) all 'Backup Set' folders which date is older than a day leaving only the latest backup set for each user. It would be simple but there are also users which are not present in company for a few days. Thus their latest backup set should stay on the server in spite of the fact that it is outdated.
I have made such a batch:
forfiles /D -1 /P D:\User /M "Backup Set *"
/S /C "cmd /c if @ISDIR==TRUE (dir /b /a:d | find /c \"Backup Set\")
| (if not %1==\"1\" (rmdir /s /q @file))"
I don't know how to pass 'dir+find' result to 'if' command which follows it.
Any help would be appreciable. As well as any other open mind suggentions regarding the topic of this question/problem.
I just asked the operating system which directory was the latest and went from there.
Batch (clunky, but it functions--it finds the newest by sorting oldest to newest, moves the folders to a temporary directory, moves the latest one back, then deletes the temporary directory):
PowerShell (find latest directory, delete directories that aren't the latest):
OK,I managed to do this using PowerShell and basing on http://nisanthkv.blog.com/2012/07/30/removing-backup-folders-solution-1
Here is the result:
Get-ChildItem MediaID.bin -Path D:\Users -Recurse | ForEach-Object {$_.DirectoryName} | ForEach-Object {Get-ChildItem "Backup Set*" -Path $_ | Sort-Object LastWriteTime -descending | Select-Object -Skip 1 | ForEach-Object {“Removing file $($_.FullName)”; Remove-Item $_.FullName -Recurse -Force}}