Clean up .net project folders before performing backup
I want to do a backup of my Projects folder. It mostly contains sub-folders for .NET projects (VS2005, VS08, and VS10).
What should I delete before doing a backup (e.g. TestResults
, bin
, obj
); if there is something I shouldn't delete but should ignore,开发者_StackOverflow中文版 what might that be; and, most importantly, how best to automate the whole process (I'm thinking batch file or better)?
What gotchas do I need to be aware of?
I would recommend using source control and backing up the source control repository. If this is just for personal projects, I would recommend either Subversion or Mercurial. They are easy to use, integrate with Windows Explorer, can be setup to ignore certain directories, and provide other well published benefits beyond backup as well.
If the source code repository is backed up, you don't need to backup the projects themselves.
As for additional things you might consider ignoring, you can safely ignore the .user and .suo files, these are just user settings like what files you have open in the project. Depending on your development environment, you can also ignore the .sln files (the solution files). The solution files are easily rebuilt and if you are working in a team environment, it might be easier to have individual solutions.
(EDIT: Looks like others, including Jeff Atwood, have created tools to do this.)
Ok, figured out that it's hard to recursively search subfolders and delete those folders matching a pattern using a batch file and cmd
. Luckily, PowerShell (which is installed on Windows 7 by default, IIRC) can do it (kudos):
get-childitem C:\Projects\* -include TestResults -recurse | remove-item -recurse -force
That was based off of example 4 of the Remove-Item
help entry. It searches the path, recursively, for anything (file or folder) named "TestResults" (could put a wildcard in there if wanted) and pipes the results to the Remove-Item
command, which deletes them. To test it out, just remove the pipe to Remove-Item
.
How do we remove more than just one folder per statement? Input the list of folders in PowerShell's array syntax:
get-childitem C:\Projects\* -include bin,obj,TestResults -recurse | remove-item -recurse -force
You can run this from the command prompt or similar like so (reference)
powershell.exe -noexit get-childitem C:\Projects\* -include bin,obj,TestResults -recurse | remove-item -recurse
Obvious Disclaimer
Don't use this method if you keep necessary files inside folders or files named bin
, obj
, etc.
精彩评论