Jump to content


[Solved] How to detect p4 edit failed in a batch file?


  • Please log in to reply
6 replies to this topic

#1 Skylark13

Skylark13

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 19 February 2020 - 04:41 AM

Hello,I have a batch file that checks out some files and then expects to be able to write to them.I want the batch file to fail clearly if the
p4 edit
command fails.For example, one user didn't have the right P4CLIENT set so it failed with "Path  is not under client root ".I would have expected p4 edit to exit with an error code, so I would be able to check if ERRORLEVEL == 0 for success or non-zero for failure. This is pretty standard for command line tools, but apparently p4 does not do this, because ERRORLEVEL stays set to 0 even after this error.I could check if the files are writable after having done p4 edit. Is that what other people do in this circumstance? Any other options?Thanks in advance,J-S

#2 Skylark13

Skylark13

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 19 February 2020 - 01:36 PM

I found another way on StackOverflow -- looks like the && operator in Windows batch files supports short-circuit evaluation.

p4 edit *.exe *.dll *.pdb && set WRITEOK=1
IF NOT DEFINED WRITEOK (
ECHO Unable to checkout files. Exiting now!
goto CheckoutError
)

This works for me. If anyone has another suggestion I'm still interested but I'll mark this question resolved.

J-S

#3 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 1037 posts

Posted 19 February 2020 - 03:34 PM

My approach is usually to define success criteria rather than check for specific errors, e.g. run a "p4 opened FILE" and check that I get a message confirming that the file is open.  There are a lot of reasons that a "p4 edit" might not open a file, with different levels of "severity" as defined by the server, but they're all fatal errors if you expect a file to be open and one didn't get opened.

#4 Skylark13

Skylark13

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 19 February 2020 - 04:29 PM

Thanks for replying. I agree that would be ideal, but checking the output is not super easy in batch files. If this were a python script that's what I would do for sure.

#5 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 1037 posts

Posted 19 February 2020 - 06:06 PM

yeah... it almost goes without saying that I don't do anything more than a couple of lines long in a batch file.  If that were a requirement I'd probably compile my own version of the p4 command line with arcane global flags that let me map specific message codes to specific exit values (one can envision a flag that says "treat any message of severity greater than E_INFO as a fatal error", or "any message that's not the specific 'opened for edit' message", etc).

Realistically I've generally used Perl for most Perforce scripting, using regexes to grab specific messages.  Nowadays I'd probably use P4Python.

#6 Skylark13

Skylark13

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 19 February 2020 - 06:56 PM

Thanks, I'll keep your suggestions in mind if our tooling ever requires more than what we can handle for the moment and it becomes too much of a pain... :)

#7 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 1037 posts

Posted 19 February 2020 - 07:54 PM

Some years ago I started a collection of all the little one-liners I was able to run from CMD without needing an actual scripting language.  The main thing all of them have in common is that they don't have any branching conditions (i.e. they don't need to take different actions based on the result of a given command):  https://swarm.worksh...s/oneliners.txt

There's also this one, which I used on a very regular basis before "p4 grep" existed: https://swarm.worksh...ipts/p4find.bat

Luckily all of that cut/sed stuff was eventually obsoleted by the "-F" flag.  :)  https://web.archive..../fun-formatting




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users