user2369546
user2369546

Reputation: 77

Perforce : Is there any command to check if client spec exist or not

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?

Upvotes: 6

Views: 7134

Answers (6)

Tom Tyler
Tom Tyler

Reputation: 1

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.)

Upvotes: 0

kdubs
kdubs

Reputation: 1722

I've found that this command works:

p4 -ztag clients -e bad

it returns nothing if the client doesn't exist

Upvotes: 4

Drew Marold
Drew Marold

Reputation: 185

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.

Upvotes: 2

pdx9k9e9
pdx9k9e9

Reputation: 61

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!

Upvotes: 5

Ricky Levi
Ricky Levi

Reputation: 8007

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 :)

Upvotes: 3

Mark
Mark

Reputation: 3700

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.

Upvotes: 2

Related Questions