How do I get the perforce root directory from command line?

11,792

Solution 1

If you want your client root in one command run:

p4 -F %clientRoot% -ztag info

You will need the 2014.1 later version of p4 to use this flag.

Solution 2

For older versions before 2014.1 use the command

p4 info | grep 'Client root:' | cut -d ' ' -f 3-

Solution 3

From OP’s description, they probably want p4 where. For example, if the client root is /perforce/projects, then

p4 where //depot/project-1/module-2

will give /perforce/projects/project-1/module-2. Credit from Bryan’s comment.

Share:
11,792

Related videos on Youtube

thisisdog
Author by

thisisdog

Updated on September 16, 2022

Comments

  • thisisdog
    thisisdog over 1 year

    How do I get the perforce root directory from the command line? I've tried p4 info but I'd rather not have to filter this to get at the root. I'm sure there's a way, but I couldn't find it.

    Is there a way to get the root in a context sensitive way? For example if I have two workspaces with a hierarchy like A/.../script vs B/.../script I'd expect that the script would return either A or B depending on where it was run from.

    • Christopher Creutzig
      Christopher Creutzig about 10 years
      I'm not sure what the problem with p4 info | grep 'Client root:' | cut -d ' ' -f 3- is that you're trying to avoid. But I never checked if, e.g., p4 info is localized – and if not, that might change in the future.
    • Bryan Pendleton
      Bryan Pendleton about 10 years
      Then it sounds like you want 'p4 where //depot/A/B/C'.
    • Franklin Yu
      Franklin Yu over 4 years
      @BryanPendleton Please promote your comment into an answer. I think a significant portion of readers of this question actually want this. Also link to manual for p4 where would be helpful.
  • Tor Klingberg
    Tor Klingberg over 9 years
    It works for me. You may have older version of the P4 client some1.
  • Matt
    Matt over 9 years
    You're correct Tor. I've updated the answer to clarify.
  • gws
    gws about 6 years
    This breaks if 'clientRoot' is already set in the environment. Escape the second percent sign: %clientRoot^%. But because CMD is special that only works if you type it yourself at a prompt. To do it in a batch file you have to say %%clientRoot^^^%%, which is pretty hideous.
  • Andreas
    Andreas about 4 years
    This works pretty fine but out of curiosity, do you know how the %clientRoot% variable is set? I tested this with 2 different roots that both have P4CLIENT set in a P4CONFIG.txt file so probably clientRoot is set through P4CLIENT?
  • Andreas
    Andreas about 4 years
    @Matt ...also, what's that -ztag parameter for?
  • Matt
    Matt over 3 years
    The -ztag tells p4 to dump out the dictionary of values sent back from the server. For a lot of commands the server sends back more info than the client uses when formatting the message