6

Actually i need write one batch script, in that first i need to check if the required client spec already exist or not? If exist then i should delete it.

Could you please let us know how can we check in a script, if the required client spec exist or not?

user2369546
  • 67
  • 1
  • 3
  • 7
  • This sooooooooooo depends on spec. Without defining that... no chance of getting an answer. – zozo Oct 28 '13 at 09:29
  • For example, i want to check the perforce client spec named, build-new, If it exist then i'll delete it with command p4 client -d build-new, – user2369546 Oct 28 '13 at 11:01
  • 1
    I got the answer, it is like below, p4 clients| findstr /i "clientname" > null, if ERRORLEVEL 0 goto echo"Client already exist" – user2369546 Oct 29 '13 at 10:50
  • You want "p4 clients -e CLIENT_NAME" answered below (in pdx9k9e9 and kdubs answers), documented in https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_clients.html – qneill Nov 04 '21 at 18:22

6 Answers6

5

On Windows command line you can do something like this

set P4CLIENT=client-name
p4 clients -e %P4CLIENT% | findstr %P4CLIENT% >nul

p4 clients -e %P4CLIENT% will output all clients matching %P4CLIENT%. findstr will search the output of p4 clients for the client name and print it. The redirection to to nul will supress this output, but findstr will additionally set the %errorlevel% variable.

Some examples:

p4 clients -e existing-client | findstr existing-client >nul
echo %errorlevel%

Will return 0.

p4 clients -e does-not-exists | findstr does-not-exists >nul
echo %errorlevel%

Will return 1.

If you want to execute something, if a given client space does not exists, you can run this command:

p4 clients -e does-not-exists | findstr does-not-exists >nul || create-client.bat

If you want to execute something, if a given client space does exists, you can run this command:

p4 clients -e does-not-exists | findstr does-not-exists >nul && do-something.bat

Thanks to Adam from the perforce online chat support!

pdx9k9e9
  • 61
  • 1
  • 2
4

I've found that this command works:

p4 -ztag clients -e bad

it returns nothing if the client doesn't exist

kdubs
  • 1,596
  • 1
  • 21
  • 36
3

Try:

p4 client -o -t $CLIENT_NAME_YOU_WANT_TO_CHECK

What this does - is trying to create a client from another client's spec as a template to my "new client".

If there's an output ( meaning $CLIENT_NAME_YOU_WANT_TO_CHECK actually exists ) - it will display it's spec as a text to the STDOUT without entering the the editor, and if the client doesn't exists - it just outputs to STDERR:

Client '$CLIENT_NAME_YOU_WANT_TO_CHECK' doesn't exist.

The "-o" print it on the screen instead of actually creating the client... nice trick, does the job for me, hope it helps :)

Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
2

I see by your comment that you found a solution, but here's another take...

If you want to delete the client by that name, you could save yourself a call to p4 clients by just trying to delete the client. If it exists, it will be deleted (unless it has shelved files, etc.). If it doesn't exist, then it's a no-op - no harm, no foul.

Mark
  • 3,609
  • 1
  • 22
  • 33
2

Do a p4 client -o client_name and check for the existence of the Update: or Access: fields. These will be set for a client that already exists, but not for one that doesn't.

If you have the 2013.2 or later version of the client, you can use formatted output to make it even easier.

p4 -ztag -F "%Update%" client -o client_name will either return a string with the date & time for an existing client or nothing for a non-existent one.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Drew Marold
  • 175
  • 6
0

Tip: Use the --exists feature with -o.

For example, try: p4 client --exists -o SomeWorkspace

If SomeWorkspace exists in the database, you'll get the client spec displayed, and a 0 exit code.

If it does not, you'll get non-zero exit code and an error message like:

Client 'SomeWorkspace' doesn't exist.

This works with various spec types (user, client, stream, etc.)