简体   繁体   中英

How can I retrieve the P4 ROOT variable value in a Windows Batch file?

I want to retrieve the p4 CLIENT ROOT variable that appears in p4 info but not sure how. I am new to batch scripts myself. I've taken a look at http://answers.perforce.com/articles/KB_Article/Accessing-Perforce-Environment-Variables-From-a-Windows-Batch-File but I changed CLIENT to ROOT but it doesn't seem to work.

EDIT: I've found a way to make the commands in that tutorial to work but that defeats the purpose of what I am trying to achieve because I have to manually set P4ROOT in the command line. Instead I am assuming I don't know the root folder so I want to find it from p4 info somehow.

As Bryan noted, the client root and the server root are two different things, and it's good to be clear on which one it is you're trying to get (it sounds like you want to get the client root for the current client; this is set in the client spec and not in any environment variable on either the client or server side). You can get them both from the output of "p4 info", though, and you can use the -F global option to p4 to isolate either field with minimal fuss:

Client root:

p4 -Ztag -F %clientRoot% info

Server root:

p4 -Ztag -F %serverRoot% info

See http://www.perforce.com/blog/130826/fun-formatting for more on the -F option.

You can parse the output of p4 set P4CLIENT to get the client name, then parse the output of p4 client clientname to get the root. Ugly, but functional:

@for /f "tokens=2 delims== " %c in ('p4 set P4CLIENT') do @(for /f "tokens=2" %r in ('p4 client -o %c ^| findstr /c:"Root:" ^| findstr /v #') do echo %r)

If you're using this in a batch file, double each of the percent signs.

EDIT - Alternately, and much easier, now that I look at it:

for /f "tokens=3" %r in ('p4 info ^| findstr /c:"Client root:"') do echo %r

Again, double the percents if you're using this in a batch file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM