Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > TFS Check if a file exists in a workspace?
Ask a questionAsk a question
 

AnswerTFS Check if a file exists in a workspace?

  • Thursday, October 29, 2009 9:59 PMSkiGeek82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Not sure I am in the correct group but I need to get this figured out ASAP.  I am creating a console app that integrates with TFS. 

    I need to know if it is possible to check if a file exists in a workspace and how it would be done.

    example command line:  checkin "d:\foo\bar\" "d:\foo\bar\comment-file" Some_File-Example.sif
    example command line:  checkout "d:\foo\bar\" "d:\foo\bar\comment-file" Some_File-Example.sif

    basically the console app runs twice in a row every time.  the first time it has the checkout action and performs the checkout.  The second time it runs the checkin and checks the file in.  So the first time I need to add the file if it does not exist and check it out or just check it out if it does exist.

    yes I know there is a command line integration built in through tf.exe but i need something with a bit more features.

    note: I cannot change the command line format.

    Thanks in advance,
    Justin

Answers

  • Wednesday, November 04, 2009 7:59 PMBuck HodgesMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Justin,

    You can use the ServerItemExists() method on VersionControlServer to check for the existence of a file given its server path.  If you need to convert a local path to a server path, you can use TryGetServerItemForLocalItem() on the Workspace class.

    I'm not familiar with the Siebel tools you describe.  If it would reuse your workspace if you ran the commands in a mapped path, that would make it more efficient.

    Buck
    http://blogs.msdn.com/buckh

All Replies

  • Friday, October 30, 2009 8:06 PMEd DoreMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Justin,

    You might need to post a more specific request. If you know where the workspace is mapped to, you can use System.File.Exists, to determine whether or not the file exists. But I suspect you need something more along the lines of calling into TFS source control to retrieve (get) the lastest file(s).

    Buck has a pretty good blog entry on accessing the TFS version control API's that might get you pointed in the right direction:

       http://blogs.msdn.com/buckh/archive/2006/03/15/552288.aspx

    Sincerely,
    Ed Dore
  • Tuesday, November 03, 2009 12:09 AMSkiGeek82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks Ed,

    You are correct, I can't use System.File.Exists.  I really want to check if the file exists in TFS.  right now what I do is try to add it and just ignore when it fails saying the file already exists (this is how the OOTB batch file works).  So checking if the file exists before adding seems to be a better approach than trying to add and seeing if it fails.

    So to get specific, I have written a command line application to replace the batch file used to integrate Siebel tools with TFS.  the batch file did not do enough and required the developer to be diligent in checking the log file.  I wanted something that was obvious if the sync failed.  I also wanted something that would remind them what branch they were checking into in tfs.  I have accomplished both of the above but ran into one more fun issue.  Siebel tools allows you to check in multiple files at the same time but the way it accomplishes this is to execute the command line app twice for every file.  Once to check out once to check in.

    How siebel tools works:

    *  Nothing is done in TFS when you checkout an object in siebel tools.  During this step siebel tools downloads the object to your local file system
    *  When you select check in siebel tools 1st checks out the object by issuing the following command to the console app:
              * checkout "d:\foo\bar\" "d:\foo\bar\comment-file" Some_File-Example.sif
                            During this step a temporary workspace is created and the file is checked out
    *  Then Siebel Tools starts the check in process by sending the following command to the console app:
              * checkin "d:\foo\bar\" "d:\foo\bar\comment-file" Some_File-Example.sif
                         During this step the updated sif file is uploaded to TFS and also uploaded to the siebel repository.  After the Upload is complete the temporary TFS workspace is deleted.  and then siebel tools deletes the local copy of the sif file.

    The issue is that Siebel tools does not wait for the previous execution of the command line to finish before the following execution begins.  So while the command line application is reminding them what branch you are about to check in to during the check out.  The check in tries to occur and fails because there are no pending changes.  I was able to get around this by having Siebel tools call a batch file that calls my command line but that just seems silly.  So any Ideas on either of my questions 1) checking to see if a file exists in siebel tools?  2) make the seccond execution of the exe wait for the first to finish?

    If you want more details, let me know. 

    Thanks,
    Justin

  • Wednesday, November 04, 2009 7:59 PMBuck HodgesMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Justin,

    You can use the ServerItemExists() method on VersionControlServer to check for the existence of a file given its server path.  If you need to convert a local path to a server path, you can use TryGetServerItemForLocalItem() on the Workspace class.

    I'm not familiar with the Siebel tools you describe.  If it would reuse your workspace if you ran the commands in a mapped path, that would make it more efficient.

    Buck
    http://blogs.msdn.com/buckh
  • Thursday, November 05, 2009 6:05 PMSkiGeek82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Buck,
    I was able to try this and worked perfectly.  Not sure how I missed it, I guess it was late and in a hurry. 
    Cheers,
    Justin