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):

│   ├─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.

  • Windows Backup is free. If it doesn't have the features you're looking for then why don't you purchase a backup program that fits your needs and keep your rant to yourself. – joeqwerty Mar 2 '14 at 21:57
  • 2
    Why are you backing up individual desktops? Why aren't you storing important files on network shares and backing those up centrally? – MDMarra Mar 2 '14 at 22:26

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):

FOR /F " tokens=*" %%i IN ('dir "c:\temp\backup*" /b /ad-h /od') DO (SET a=%%i) 
mkdir temp
FOR /F " tokens=*" %%i IN ('dir /b /ad-h /od') DO (move "%%i" temp)
cd temp
move "%a%" ..
cd ..
rd /s /q temp 

PowerShell (find latest directory, delete directories that aren't the latest):

$latest = get-childitem c:\temp\backup* | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
Get-ChildItem -Path  'C:\temp\backup*' -Recurse | ? { $_.PSIsContainer } | Select -ExpandProperty FullName | Where {$_ -notlike $latest} |sort length -Descending | Remove-Item -force 
| improve this answer | |

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}}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.