Geeks With Blogs

(Amazon Affiliate Ads)

Mike Parks Code.Where(c => c.IsTechnicalDebt).Refactor();


For Visual Studio 2010 / TFS 2010 please see:

In one of our meetings the other day a few developers on our team asked if there was an Auto Shelve feature in TFS to shelve their pending changes automatically each night. I couldn't find anything in the Visual Studio 2008 IDE that allowed us to do this.  After some research, I did find a work around.

Basically, I just created a batch file that uses the Visual Studio 2008 Command Prompt to run tf shelve on the highest level folder of your local TFS workspace mapped on your C drive. It uses tf shelve to create a TFS shelveset called "Daily Backup - Your Name".  It will replace that same shelveset every day with all your current changes (or however often you have the Windows Scheduled Task runs).

This way, you will always have a backup copy of your daily changes. It's always nice to be on the safe side, even if you think you don't need it.  A few developers have had their machines crash on them and had to have the machine re-imaged. This would have came in handy back then.  I could have built a custom TFS add-in or app, but I figured this was extremely easy to just use a batch file and windows task.  Anyway, to set this up, just follow the steps below:

  1. Open notepad and paste these two lines in:

    call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
    tf shelve "Daily Backup - YOUR_NAME" /noprompt /replace > "C:\TFSBackupLogFile.txt"
  2. Edit the title in the 2nd line to include your name and edit the path at the end of the 2nd line to map the log file
  3. Save the file to your desktop (or anywhere you'd like) and name it TFSBackup.bat
  4. Click Start > Control Panel > Scheduled Tasks > Add Scheduled Task
  5. Walk through the scheduled task Wizard and point it to the TFSBackup.bat file you created
  6. Set the Task to run daily at whatever time you want
  7. Run the task to make sure it runs executes the bat file successfully and check TFS to make sure your shelveset was created

You can view TFS Shelvesets by opening Visual Studio > File > Source Control > Unshelve Pending Changes.




Posted on Tuesday, September 15, 2009 11:14 PM | Back to top

Comments on this post: TFS Auto Shelving

# re: TFS Auto Shelving
Requesting Gravatar...
I actually use such a batch file already for a year. I use this command:

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" shelve /replace /recursive /noprompt "AUTOSHELVE_%date:~0,2%%time:~0,2%" c:\dev

The name of the shelveset is composed with the 2-letter day of the week name and the hour of the day. I run this command every hour with the Task Manager. Thus I have rolling backups every hour, which will be overriden after a week.
Left by Jaap on Sep 18, 2009 1:59 AM

# re: TFS Auto Shelving
Requesting Gravatar...
Thanks for the tip Jaap! I'll give that a shot. It sounds really helpful.

- mike
Left by Mike Parks on Sep 18, 2009 9:46 AM

# re: TFS Auto Shelving
Requesting Gravatar...
How do I identify the server and workspace within the bat file? If I run your example I get error "Unable to determine the workspace."
Left by Russ on Aug 17, 2010 2:06 PM

# re: TFS Auto Shelving
Requesting Gravatar...
I think I had that same problem at one point on my old machine and I had to add an extra line to the bat file so it would run in the directory that my TFS workspace was mapped to.

If you open Visual Studio and click File > Source Control > Workspaces from the main menu, it should bring up a window that allows you to view your workspace mappings.

I added a line at the beginning of the bat file like this:

So I think the whole bat file would look something like this:
cd C:\TFS
call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
tf shelve "Daily Backup - YOUR_NAME" /noprompt /replace > "C:\TFSBackupLogFile.txt"

I haven't tried it out since I don't get that error anymore with my new machine but that fixed it for me when I had my old box. Hope that helps!

- Mike
Left by Mike Parks on Aug 18, 2010 9:56 AM

# re: TFS Auto Shelving
Requesting Gravatar...
I always want to make backups to my folders but I keep on forgetting to do this, hoping my computer will never crash. The TFS shelveset seems like a good plan, it's like a car cover for the rainy days. I'll give it a go as soon as I can. Thanks for the article!
Left by Heidi Lavern on Nov 25, 2010 7:51 PM

Your comment:
 (will show your gravatar)

Copyright © MikeParks | Powered by: