Showing results for 
Search instead for 
Did you mean: 
Showing results for 
Search instead for 
Did you mean: 

We are working to address an issue with subscription email notifications. In the meantime, be sure to check your favorite boards for new topics.

Script Help - Solution


Script Help - Solution

Ok, I gave up on doing this with standard windows commands.

I installed Perl, and had a guy here that knows it well, help me get the output I wanted. (I've got to learn Perl!)

Here's the command using Perl to format the output:
"C:\ptc\Creo 2.0\Parametric\bin\myptcstatus" | perl -ne "print scalar(localtime(time)), \"\t\", $1, \"\t\", $2, \"\n\" if $_ =~ /\s+(PROE_Flex3C)\s+(\d+)\s+\d+/ ">>c:\PTC\FLEX3C_data.txt

It outputs this, with tabs between the elements so it can be imported easily into Excell. (import using delimiters, tabs)
Mon Mar 3 10:19:43 2014 PROE_Flex3C 5

You can change what it's looking for to output usage info for other licenses. Like this:
C:\Users\haigh1>"C:\ptc\Creo 2.0\Parametric\bin\myptcstatus" | perl -ne "print scalar(localtime(time)), \"\t\", $1, \"\t\", $2, \"\n\" if $_ =~ /\s+(Mathcad)\s+(\d+)\s+\d+/ ">>c:\PTC\Mathcad_data.txt

The myptcstatus command is a copy of the ptcstatus.bat file with this information removed: (Edit with notepad++ to see line numbers.)

Line #
69 %status_cmd% %forcea% %1 %2 %3 %4 %5 | more %more_flag%
70 pause

The Perl we used was ActivePerl.
Just add a pipe and the name you want to look for. You can add as many as you want. Your output will look like this:
Mon Mar 3 10:13:34 2014 PROE_Flex3C 5
Mon Mar 3 10:13:34 2014 Mathcad 6

The advantage of this over M. Fischer's ptcstat script, is it doesn't have to be installed on the server, you don't have to install IIS, and it's not confused by reserved licenses. Also it's only one line! You can put as many lines in a batch file as you want to track any license feature you want, outputting separate data files for each. All you need is an "at" command to run your batch file periodically.

David Haigh
This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.

Hi David

You could obtain the same output with a simple batch file (example below, similar to what I had sent you).

This runs directly in windows without perl.

It's possible to pass a list of servers or license codes if required, in my case I have a folder of textfiles, each one named after a license code, containing a list of ports and server names. The batch file loops through these and reports usage, allows us to track a wide range of software, and it is simple to make updates.

This feeds into CADminTools, which I will re-launch this summer with some updates, still open-source!



call lmutil lmstat -c 7788@%SRV% -f %LIC% > temp.1
find "issued" temp.2
for /F "tokens=3,6,11 delims=: " %%i in (temp.2) do echo %date%%time%%%i%%k%%j

Oh no, you give up too easy!

First of all, your initial command was very close to working. A few
things were wrong with it. It is supposed to look like this:
FOR /F ["options"] %variable IN (`command`) DO command
as "FOR /?" will show you.
What you did wrong:
1. The single quotes in command are back quotes (the ones in top left
of a US keyboard, not the ones just left of the enter key) if you use
that usebackq option.
2. You forgot the IN
3. The pipe | needs to be escaped. A quick Google for "pipe escape
batch" learns that using "^|" does the trick.

Also a number 4 for ease: if you do a "ptcstatus.bat /nopause" you
don't need to copy and edit it.

The working command then:
FOR /F "tokens=2 usebackq" %A IN (`ptcstatus.bat /nopause ^|findstr
Flex3C`) DO set FLEX3C=%A
FOR /F "tokens=2" %A IN ('ptcstatus.bat /nopause ^|findstr Flex3C') DO
set FLEX3C=%A

By the way if you wish to do calculations with that variable, you
should use "set /A FLEX3C=%A".
See "set /?" for more help.

Second... ditch the Perl stuff, ASAP! I've been there and done that.
First it seems great cause you can do just about anything with Perl. But
then, half a year later, you wish to make some modifications and you
cannot make sense of the Perl garble anymore (unless you use it on a
daily basis).

If you really think you need added capabilities to do some
scripting/programming, you should consider using Python instead. It is a
well structured language, can do everthing you will ever want to do with
it, and you can still read the code after you've not looked at it for a
year. Plus if you want to go really fancy, you can use the PyWin32
extension to use Excel or Word (or any other software, as long as there
is documentation for it) from *within* Python. So you can skip that
Visual Basic course too, as an added extra 😉

If you have any questions, let me know.

Best regards,
Patrick Asselman

Ok, being able to escape the pipe symbol helps. However still have problems with the command. And variable results using ‘ or `.

This next command uses the single quote that is the lower case of the double quote on the keyboard. It works but it’s not specific enough. Using the other quote, doesn’t work.

FOR /F "tokens=2" %A IN ("C:\ptc\Creo 2.0\Parametric\bin\ptcstatus.bat" /nopause ^|findstr Flex3C') DO set FLEX3C=%A

set FLEX3C=6

set FLEX3C=PROE_Flex3C <-- finding the reserved licenses. Repeats this for each reserved license. 29 times in my case.

So then used the up arrow to edit the command and add the exact string modifier. I get this error:

FOR /F "tokens=2" %A IN ("C:\ptc\Creo 2.0\Parametric\bin\ptcstatus.bat" /nopause ^|findstr /C:" PROE_Flex3C") DO set FLEX3C=%A

'C:\ptc\Creo' is not recognized as an internal or external command,

operable program or batch file.

Changing the single quote to the lower case of the ~ key give this error.

FOR /F "tokens=2" %A IN (`"C:\ptc\Creo 2.0\Parametric\bin\ptcstatus.bat" /nopause ^|findstr /C:" PROE_Flex3C"`) DO set FLEX3C=%A

The system cannot find the file `"C:\ptc\Creo 2.0\Parametric\bin\ptcstatus.bat".

David Haigh

One more thing on this. When I set up a scheduled task, it appeared to run, but was not outputting any data to the files.

I used this command to create the task:
schtasks /create /sc minute /mo 15 /ru "System" /tn "Track PTC Licenses" /tr C:\PTC\Track_licenses.bat

The problem was the scheduled task is running as a different user and didn't recognize perl as a program. Adding the full path in the script solved the problem.

"C:\Program Files\flexnet\bin\myptcstatus" | "C:\Perl64\bin\perl.exe" -ne "print scalar(localtime(time)), \"\t\", $1, \"\t\", $2, \"\n\" if $_ =~ /\s+(PROE_Flex3C)\s+(\d+)\s+\d+/ " >> c:\PTC\tracking\data-FLEX3C.txt

David Haigh

Yes I'm afraid that despite all the patches over the years, batch
scripting is still very limited due to its poor design, and you quickly
run into its limitations.

You could still work around it, for instance by writing the ptcstatus
output to a file, and then use findstr on that file.

But at some point, the effort needed to work around Microsoft's bugs is
no longer worth it, and it is better to switch to a proper scripting
language, such as Python 😉

Best regards,
Patrick Asselman

Top Tags